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

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Object vs reference !

Hey Java-fans,

I am stuck with a basic java topic. What is the differene between an object and a reference?

I know this is superbasic, but I canΒ΄t get it in my brain :)

Grigorij

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hey Grigorij,

A reference is the object variable which is directly linked to the object we create. Any changes we make to the variable will reflect onto the object itself. For example:

Cat myCat = new Cat();

What we did here is create a new Cat object. Now, we can 'reference' to the object we created using the 'myCat' variable. We can call methods on the myCat variable and it will directly affect our Cat object we created

Kevin

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

HI Kevin,

so myCat is the reference and Cat is the object? Or myCat reference a Cat object?

But is myCat a reference variable?

Kevin Faust
Kevin Faust
15,353 Points

Yep! We created a new Cat object and we can reference to it by using the myCat variable. So yes myCat is a reference variable to the Cat object. myCat now basically has all the propterties and methods of the Cat class and we can use them simply by refering to the myCat variable.

Does that make sense?

Kevin Faust
Kevin Faust
15,353 Points

This concept will start to make more sense and become second nature to you eventually as you progress :)

Best of luck!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

I hope so, Kevin

Thx

Greets from germany :)

Kevin Faust
Kevin Faust
15,353 Points

Greets from Canada :)

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Ha Ha,

cool:) I have been on honemoon in Canada for a whole month 2 weeks ago. We visited British Columbia and all national parks. Beautifull Beautifull Beautifull

Grigorij

Kevin Faust
Kevin Faust
15,353 Points

Must've been a great time! Congratulations on your wedding :)

Richard Lu
Richard Lu
20,185 Points

Hey Grigorij,

To explain this, lets look at simple analogy. Lets picture an ice cube tray, and every time we wanted to create an object called ice cube (IceCube), we fill one of the cublets up.

Once there's at least one ice cube, we're able to reference the ice cube(s) by where they're located (location in ice cube tray). The location of the ice cube in this case is the name of the object.

IceCube firstCube = new IceCube()   // a newly created ice cube
IceCube secondCube = new IceCube()  // another ice cube

to reference these ice cubes, just call their name.

firstCube.putInDrink()     // go to the location of firstCube in the ice cube tray and put it in a drink
secondCube.putInDrink()

In a real world example, the ice cube tray represents memory.

Hope I've helped. Happy coding :)