my dog learned polymorphism*
The moose likes Beginning Java and the fly likes static variable memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variable memory" Watch "static variable memory" New topic
Author

static variable memory

Eshwar Prasad
Ranch Hand

Joined: Mar 21, 2008
Posts: 202

In one of the examples i see the below code.



From the code, I understand like when the class is invoked, a new object of the class in created on the heap. As it is at class level and static, when will the object get garbage collected. Should I reference it to NULL once it is used?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Eshwar Prasad wrote:
In one of the examples i see the below code.



From the code, I understand like when the class is invoked, a new object of the class in created on the heap.


Classes do net get invoked. A class gets loaded and initialized, which only happens the first time your code uses it. A class also gets instantiated, meaning an instance of that class (an object) is created. This happens when you use the new operator.

As it is at class level and static, when will the object get garbage collected. Should I reference it to NULL once it is used?


Objects cannot be "class level" or "static". Those properties do not apply to objects, only to variables and methods. The object that your static variable refers to will become eligible for GC when it is no longer reachable, just like any other object. There is almost never a need to "help" the GC by explicitly setting a reference to null, and there's certainly no reason for you to do it here.
Jason Hardaway
Greenhorn

Joined: Apr 11, 2011
Posts: 26

Eshwar Prasad wrote:
when will the object get garbage collected. Should I reference it to NULL once it is used?


The JVM will decide when to run garbage collection depending on when it thinks it will need more heap size. However, I do not remember how the JVM decides this at the moment because I do not have my SCJP book with me. Also, you can request that the JVM run the Garbage Collector by invoking the System.gc() method but it is not guaranteed that the JVM will honor your request but I believe that it will most of the time.

If I am reading your second question correctly, as for referencing it to null, the object is automatically eligible for GC as soon as its reference variable is null. Here are more resources for you. http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html

http://www.coderanch.com/t/263571/java-programmer-SCJP/certification/islands-isolation-Garbage-Collection

Hope this helps.
Jason Hardaway
Greenhorn

Joined: Apr 11, 2011
Posts: 26

Jason Hardaway wrote:
Eshwar Prasad wrote:
when will the object get garbage collected. Should I reference it to NULL once it is used?

Also, you can request that the JVM run the Garbage Collector by invoking the System.gc() method but it is not guaranteed that the JVM will honor your request but I believe that it will most of the time.

I forgot to mention that it is not recommended that you run System.gc(). As Jeff stated, you are not required to help the JVM because it will make the final decision anyway.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jason Hardaway wrote:
the object is automatically eligible for GC as soon as its reference variable is null


That statement is inaccurate because it implies that each object has one reference variable inherently associated with it. An object can have zero, one, or multiple references to it, and not all those references will necessarily be in variables.

An object is eligible for GC when it is no longer reachable from any "root reference."
Jason Hardaway
Greenhorn

Joined: Apr 11, 2011
Posts: 26

Jeff Verdegan wrote:
Jason Hardaway wrote:
the object is automatically eligible for GC as soon as its reference variable is null


That statement is inaccurate because it implies that each object has one reference variable inherently associated with it. An object can have zero, one, or multiple references to it, and not all those references will necessarily be in variables.

An object is eligible for GC when it is no longer reachable from any "root reference."


Thank you, I didn't realize I stated it that way. Yes, you are correct when you say that all references do not have to be in variables because a class can have an instance variable that is a reference variable to another instance of the same class.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
On a separate note, lots of doubts regarding the usage of static probably arise due to the fact that static members in a class can be accessed via object references. I'm still stumped by it now and then! Can anybody point out why this usage although mentioned as wrong in every Java book, was never restricted from the JLS itself?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jason Hardaway wrote:
Thank you, I didn't realize I stated it that way. Yes, you are correct when you say that all references do not have to be in variables because a class can have an instance variable that is a reference variable to another instance of the same class.


That's not what I was talking about, and that is still a reference in a variable. I meant, for example, something like this:

Here, there is a reference to the newly created object in our bar() method, but it's not in a variable.
Jason Hardaway
Greenhorn

Joined: Apr 11, 2011
Posts: 26

Jeff Verdegan wrote:
That's not what I was talking about, and that is still a reference in a variable. I meant, for example, something like this:

Here, there is a reference to the newly created object in our bar() method, but it's not in a variable.

Thanks, I think I understand what you were saying. According to the Java Tutorials, http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
The 'new' operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type. However, the reference returned by the new operator does not have to be assigned to a variable.
In the code sample below, I was able to access the instance fields using the 'new' operator. Although the sample is different from the one you provided, I believe the concept is the same.

In this next sample, I was able to access the fields using the 'new' operator and also reference variables.

I knew that the fields could be accessed by using the reference variables (such as baz.b) but I originally thought that the fields could be accessed through the 'new' operator only because the instance was created (and the constructor is invoked). However, I did not know that the 'new' operator actually returned an object reference when it created the instance. I missed that part in my studying
I think this is what you were trying to point out. I am still a beginner so is this what you were trying to say?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jason Hardaway wrote:
I knew that the fields could be accessed by using the reference variables (such as baz.b) but I originally thought that the fields could be accessed through the 'new' operator only because the instance was created (and the constructor is invoked). However, I did not know that the 'new' operator actually returned an object reference when it created the instance. I missed that part in my studying
I think this is what you were trying to point out. I am still a beginner so is this what you were trying to say?


Yeah, that's pretty much it.
Jason Hardaway
Greenhorn

Joined: Apr 11, 2011
Posts: 26

Jeff Verdegan wrote:
Yeah, that's pretty much it.

I understand now. Thanks.
 
 
subject: static variable memory