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 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 &&:)