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

i am not able to access MAX_PEZ even after it is declared static in java-perl

Even After declaring MAX_PEZ as static in the class whenever i run the following command to access it PezDispenser.MAX_PEZ

ERROR: not a statement
PezDispenser.MAX_PEZ;
^

ERROR: illegal start of type
java.lang.Object $JAVAREPL_EXPRESSION_VALUE$ =
^
ERROR: ';' expected
java.lang.Object $JAVAREPL_EXPRESSION_VALUE$ =
^

This is my code:

public class PezDispenser{ public static final int MAX_PEZ = 12; private String mCharacterName; : public PezDispenser(String characterName){

mCharacterName = characterName;

}

public String GetCharacterName(){ return mCharacterName;

} }

1 Answer

James Vaughan
PLUS
James Vaughan
Courses Plus Student 4,462 Points

Amith,

I assume you mean java-repl rather than perl. I created a PezDispenser.java file from your source as follows:

public class PezDispenser {

    public static final int MAX_PEZ = 12;
    private String mCharacterName;

    public PezDispenser(String characterName) {
        mCharacterName = characterName;
    }

    public String GetCharacterName() {
        return mCharacterName;
    }
}

I was able to create a PezDispenser object and access the MAX_PEZ constant. The following was the output I saw:

java> :load PezDispenser.java                                                                                              
Loaded source file from PezDispenser.java                                                                                  
java> PezDispenser dispenser = new PezDispenser("Yoda");                                                                   
PezDispenser dispenser = PezDispenser@1e8d17d1                                                                             
java> dispenser.MAX_PEZ;                                                                                                   
java.lang.Integer res1 = 12                                                                                                
java>   

I'm not sure then why you're seeing an error as your code appears to work okay. I suspect the error is in how you are calling the method. You need to first instantiate the object, then call the method against that (as my example above).

Hope that helps.