• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference Between the Reference Variable and an Object

 
Ranch Hand
Posts: 37
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I have a small doubt, according to the code below



The expected output of the above code is: NPE(NullPointerException) because since now class1 reference variable
is referring to the class2 object, and class2 object has been made null

but it is showing the output as The class 1 is:test.NewClass@2d6d03ba

can someone please explain me this


Thanks a lot in advance,
Siddarth
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 6, class2 refers to an object, NewClass.
In line 7, class1 refers to class2, which is also referring the object created on line 6.
But line 8, class2 refers to null. What happens to class1? class1 refers to NewClass object created in line 6.
 
Siddarth Cherukumudi
Ranch Hand
Posts: 37
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, for your response,
but my doubt here is

class1 is referring to the object which is no more existing, doesent that cause NPE

please shed some light on this..

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siddarth Cherukumudi wrote:Thank you, for your response,
but my doubt here is

class1 is referring to the object which is no more existing, doesent that cause NPE

please shed some light on this..



As mentioned in the subject, reference variables and the objects that they refer to are two different things. Don't mix them up.

When two different reference variables are pointing to the same object, it just means that the two variables are pointing to the same object. That is all. It doesn't mean that when you change one of the reference variables to point to something else (or in this example, point to nothing), that the other will change. Setting one reference variable doesn't change the other.

Henry
 
Siddarth Cherukumudi
Ranch Hand
Posts: 37
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot again, for your patience

please correct me if i am wrong,

so practically, class1 and class2 are two different variables

so changing one of them will definitely not change the other, is that right?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's right. Think of it this way: You and I are both pointing at a cup on your table. I turn around and start pointing at the window. What are you pointing at now? Well, the cup of course. It's the same with reference variables, they point at objects.
 
Siddarth Cherukumudi
Ranch Hand
Posts: 37
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks a lot guys,

this is my first post in this forum

and can you please help me with some tutorial or
post regarding garbage collection for scjp6 exam

really having tough time understanding that concept
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic