Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Harnessing the Power of Objects Methods and Constants

Sean Flanagan
Sean Flanagan
33,235 Points

Console

Hi. I copied Craig's code in the console but got an error message:

java> :load PezDispenser.java                                                         
Loaded source file from PezDispenser.java                                             
java> PezDispenser.MAX_PEZ                                                            
ERROR: not a statement                                                                
    PezDispenser.MAX_PEZ;                                                             
                ^                                                                     

ERROR: non-static variable MAX_PEZ cannot be referenced from a static context         
    PezDispenser.MAX_PEZ;                                                             
                ^                                                                     
java> 

Can anyone tell me please where I've gone wrong?

Thanks

Sean

2 Answers

You most likely forgot to make MAX_PEZ static. If that's not the case, please include your code.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Kristian.

Here's my code for PezDispenser.java:

public class PezDispenser {
  public static final int MAX_PEZ = 12; /* public means visible to everyone
  int means integer (whole number); constant means values that don't change, all capitals,       underscore separates words final: prevents value from being changed 
  static: members belong to class, not to specific instance, ergo only one instance of static field exists*/
  private String mCharacterName;
  private int mPezCount; //declared but uninitialised 

  public PezDispenser(String characterName) {
    mCharacterName = characterName;
    mPezCount = 0; //Pez dispenser comes empty
  }

  public void load { /* Load method public for exposure.
  void: Method that returns nothing */
    mPezCount = MAX_PEZ; // mPezCount set to maximum number of pez
  }

  public String getCharacterName() {
    return mCharacterName;
  }
}

And for Example.java:

public class Example {

    public static void main(String[] args) { 
        // main: When you run this file, it searches the class for main and runs example.main.
        // args: Allows you to pass in arguments along the command line.
        // Your amazing code goes here...
        System.out.println("We are a making a new Pez Dispenser.");
        PezDispenser dispenser = new PezDispenser("Yoda");
        System.out.printf("The dispenser character is %s\n",
                          dispenser.getCharacterName());
    }
}

Don't worry about the comments; these are for my benefit. I restarted the video, deleted the workspace and opened a new one and it seems to be working okay now.

Thanks.

Sean :-)