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 trialGrigorij Schleifer
10,365 PointsObject 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
15,353 PointsHey 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
Richard Lu
20,185 PointsHey 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 :)
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHI 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
15,353 PointsKevin Faust
15,353 PointsYep! 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?
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsCool,
thanks Kevin :)
Grigorij
Kevin Faust
15,353 PointsKevin Faust
15,353 PointsThis concept will start to make more sense and become second nature to you eventually as you progress :)
Best of luck!
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsI hope so, Kevin
Thx
Greets from germany :)
Kevin Faust
15,353 PointsKevin Faust
15,353 PointsGreets from Canada :)
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHa 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
15,353 PointsKevin Faust
15,353 PointsMust've been a great time! Congratulations on your wedding :)