Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2285 > unrolled thread

static context puzzle

Started byRoedy Green <see_website@mindprod.com.invalid>
First post2012-11-28 03:18 -0800
Last post2012-11-28 04:21 -0800
Articles 2 — 1 participant

Back to article view | Back to comp.lang.java.help


Contents

  static context puzzle Roedy Green <see_website@mindprod.com.invalid> - 2012-11-28 03:18 -0800
    Re: static context puzzle Roedy Green <see_website@mindprod.com.invalid> - 2012-11-28 04:21 -0800

#2285 — static context puzzle

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-11-28 03:18 -0800
Subjectstatic context puzzle
Message-ID<eksbb851shsvfnllarvd02dt56eenlltt7@4ax.com>
I have prepared the following SSCCE to demonstrate a puzzle.
The compiler is complaining about using an instance method in a static
context. It looks fine to me.  What am I overlooking?



/*
 * [EnumSSCCE.java]
 *
 * Summary: Demonstrate a  problem with static context and enum.
 *
 * Copyright: (c) 2012 Roedy Green, Canadian Mind Products,
http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any
purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: JetBrains IntelliJ IDEA IDE
http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2012-11-28 initial version
 */
package com.mindprod.test;

import java.io.File;

public enum EnumSSCCE
    {
        CLIMATECRISIS( 1, 2 )
                {
                String ad( final File fileBeingDistributed, final File
footerFile )
                    {
                    // error: non-static method
chooseAnAdVariant(File) cannot be referenced from a static context
                    // why? ad is instance as is chooseAnAdVariant
                    // Why does it consider this spot in the code a
static context?
                    final int choice = chooseAnAdVariant(
fileBeingDistributed );
                    switch ( choice )
                        {
                        case 0:
                            return "HTML for choice 0";

                        case 1:
                            return "HTML for choice 1";

                        default:
                            throw new IllegalArgumentException(
"program bug: invalid ad number" );

                        }
                    }
                };

    // ------------------------------ FIELDS
------------------------------

    /**
     * how many different variants there are on this ad.
     */
    private final int variants;

    /**
     * weight for this charity in determining frequency of ad
placement.
     */
    private final int weight;

    // --------------------------- CONSTRUCTORS
---------------------------

    /**
     * constructor
     *
     * @param weight relative weight to control the frequency this ad
appears
     */
    EnumSSCCE( final int weight, final int variants )
        {
        this.weight = weight;
        this.variants = variants;
        }

    // -------------------------- OTHER METHODS
--------------------------

    /**
     * get html public service ad
     *
     * @param fileBeingDistributed page where ad will be embedded
     * @param footerFile           web page where iframe ad will go
     *
     * @return html to display banner and link
     */
    abstract String ad( final File fileBeingDistributed, final File
footerFile );

    /**
     * select which of the ad variations to use
     *
     * @param fileBeingProcessed page where ad will be embedded
     *
     * @return choice 0..variants-1
     */
    private int chooseAnAdVariant( final File fileBeingProcessed )
        {
        return variants - 1;
        }
    }
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish 
as couch potatoes who hire others to go to the gym for them. 

[toc] | [next] | [standalone]


#2286

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-11-28 04:21 -0800
Message-ID<mutbb8l9c392t5ocrm952p59out7hcnka8@4ax.com>
In reply to#2285
On Wed, 28 Nov 2012 03:18:25 -0800, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>I have prepared the following SSCCE to demonstrate a puzzle.
>The compiler is complaining about using an instance method in a static
>context. It looks fine to me.  What am I overlooking?

The secret to making it work is to remove the keyword "private"
from 

private int chooseAnAdVariant( final File fileBeingProcessed 

The error message is misleading. Inner classes cannot see the parent's
private methods.  Methods attached to individual enum constants are
inner classes of the enum as a whole under the hood.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Students who hire or con others to do their homework are as foolish 
as couch potatoes who hire others to go to the gym for them. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web