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 (Retired) Meet Objects Welcome Back

Alexander Schott
Alexander Schott
8,318 Points

Why first variable stored in res2, not in res1 at (7:09)?

As Craig cleared the screen and starts REPL again, the variables should be lost. He created a boolean variable res2 by using a method with return value and he doesnt explicit make one, so REPL overtakes this. Why is there no res1 created? I thought every variable is lost? Do you know what i mean? I am just curious, its not a conceptuel question.

2 Answers

Maciej Torbus
Maciej Torbus
8,137 Points

Hey Alexander!

Good thing that you are curious! Take a look at this example which I've just created on javarepl.com

http://oi61.tinypic.com/1e6lcn.jpg

It seems that the variables starts at res0, so in Craig's example the two ints ageOfBob (0) and ageOfMary(1) are res0 and res1 and that's why the boolean comes with res2.

And yes, all the variables were cleared and lost there. That's good point.

Hope it helps!

Alexander Schott
Alexander Schott
8,318 Points

thank your for that fast reply! Ah so every variable gets internally also stored in res0-res... even if you declare the variable by names. But you cant access them by res0, for example if you created your first variable like String aStr = "something";.

Maciej Torbus
Maciej Torbus
8,137 Points

That sounds right. I'm not familiar with java-repl but notice that using res variables could teach you some bad practice. Notice in the example that "1 > 0" in normal case would give you an error because you should store it in some variable. Java REPL does that for you and creates res0. You can't access it, because you should name your variables. Good practice is writing: boolean check = 1 > 0;

In example you gave, you should always access your variable by using its name (aStr), remember those res are just for your help/learn

That's what I think I'm not sure I'm right all the way - never used Java REPL before

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

If you type :reset it will clear the variables. :clear just clears the screen, so will Ctrl+L

Also, don't pay too much attention to the variable names there, they are just part of the tool, in case you ever want to use them again.