• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static object for garbage collection

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have the following code:


Is the Class1 object created on Line 1 eligible for garbage collection? I don't think so because it was assigned to a static Class1 variable which means it is still accessible by Class1.c1. Am I right? Thanks!

 
Paul Clapham
Sheriff
Posts: 21554
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right.

(As far as garbage collection goes, it's right. But please don't call it a "static object", because there's no such thing. That terminology can easily lead to confusion. Your post shows that you know it's all about the references, and what you have there is a static variable containing a reference to an object.)
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Paul Clapham
Sheriff
Posts: 21554
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing which can lead to confusion is this:



It's bad practice to assign something to a static variable of a class via an instance of a class. This code is exactly equivalent to



But if the test writers had written it that way, the question would have been much easier, wouldn't it?

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what do you think will be the result of this code?
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since a1 is null wouldn't a1.c1 give a null pointer exception? Also, you are correct Paul, the test writers try to be subtle (or tricky!).
 
Paul Clapham
Sheriff
Posts: 21554
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:Since a1 is null wouldn't a1.c1 give a null pointer exception?


A good question. No, it wouldn't throw an exception because the compiler automatically fixes it up to use the Class object version. Try running that code and see what happens.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:Since a1 is null wouldn't a1.c1 give a null pointer exception?

Think about Paul's post
Paul Clapham wrote:Another thing which can lead to confusion is this:



It's bad practice to assign something to a static variable of a class via an instance of a class. This code is exactly equivalent to



 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Any class reference variable can be used to access a static variable regardless of whether it refers to an object or not. So your code would print out the static variable c1 which contains the address of the Class1 object. So you would get a memory address printed out. Correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:So your code would print out the static variable c1 which contains the address of the Class1 object. So you would get a memory address printed out. Correct?

First part is correct, 2nd isn't.

From javadocs of the toString method in the Object class:
Object.toString wrote:The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just to illustrate this little code snippet:A possible output:
Class1@734bcb5c
734bcb5c
1934347100
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic