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


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

static context puzzle

From Roedy Green <see_website@mindprod.com.invalid>
Newsgroups comp.lang.java.help
Subject static context puzzle
Date 2012-11-28 03:18 -0800
Organization Canadian Mind Products
Message-ID <eksbb851shsvfnllarvd02dt56eenlltt7@4ax.com> (permalink)

Show all headers | View raw


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. 

Back to comp.lang.java.help | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web