Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Meet Objects Access Level Modifiers

"characterName" is still being accessed by Example.java

As far as I can tell, my code is exactly what Craig has, but my console output is still:

Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
We are making a new PEZ Dispenser
The dispenser is Yoda

Here's my code (I'm copying and pasting...not sure how to attach screenshots, also not sure how to format it and make it pretty)

PezDispenser.java:

class PezDispenser {
    private String characterName = "Yoda";
}

Example.java

public class Example {

  public static void main(String[] args) {
    // Your amazing code goes here...
    System.out.println("We are making a new PEZ Dispenser");  
    PezDispenser dispenser = new PezDispenser();
    dispenser.characterName = "Darth Vader";
    System.out.printf("The dispenser is %s %n",
                      dispenser.characterName);
  }
}

Note that even though I have dispenser.characterName = "Darth Vader"; my console still says The dispenser is Yoda, and adding private to PezDispenser.java didn't prevent Example.java from accessing it.

What am I missing?

Moderator edited: Added markdown to make the code render properly on the forums.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Alex! I took the liberty of adding some markdown to your code so that it'll be more readable by other students. At the bottom :arrow_down: of the "Add an Answer" section is a link to the Markdown Cheatsheet with instructions and tips on how to format your posts on the forums :sparkles:

Could this be an issue with Workspaces? Does it ever cache? I continued with the next lessons (Methods). I changed PezDispenser.java to:

class PezDispenser {
    private String characterName = "Yoda";

    public String getCharacterName() {
        return characterName;
    };
}

and Example.java to:

public class Example {

  public static void main(String[] args) {
    // Your amazing code goes here...
    System.out.println("We are making a new PEZ Dispenser");  
    PezDispenser dispenser = new PezDispenser();
    System.out.printf("The dispenser is %s %n",
                      dispenser.getCharacterName()
                     );
  }

}

And it worked, as expected. But then I changed "Yoda" to "Luke" (in PezDispenser.java), and my console output still gave me:

The dispenser is Yoda

1 Answer

I found a workaround. In the console, I was doing

clear javac Example.java && java Example                                                                            

And found that if I do javac Example.java and java Example separately my code runs fine.

Same problem i dont think workspace is very reliable.

Teacher Russell
Teacher Russell
16,873 Points

clear && javac Example.java && java Example. You missed an &&:)