/* * [EnumSSCCE.java] * * Summary: Demonstrate a problem with static context and enum. * * Copyright: (c) 2012-2017 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.8+ * * 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; /** * Demonstrate a problem with static context and enum. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2012-11-28 initial version * @since 2012-11-28 */ 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" ); } } }; /** * 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; /** * 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; } /** * 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 * DO NOT MAKE PRIVATE */ int chooseAnAdVariant( final File fileBeingProcessed ) { return variants - 1; } }