aspose file tools*
The moose likes Beginning Java and the fly likes static field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static field" Watch "static field" New topic
Author

static field

dennis liang
Greenhorn

Joined: Dec 15, 2006
Posts: 9
Hi,
when you first time access the static object in a class. The class is loaded and static object is initialized. It will not be reinitialized.
If you don't use it again, does the garbage collector will release the memory like non static object? or does it always stay in the memory until the program finishs? If it is running on a server, does it always in the memory and ready to be used until you stop the server?
Thanks
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I jumped in the middle of a big discussion about this and it was generally the consensus that trying to use the gc results in problematic issues that really only an experienced engineer can approach effectively and reliably. If your code just left a large chunk of work and you know that the program will probably not be busy for a awhile, then you can put a call to gc there if you want to but I do not use it at all after that discussion.

Static anything is probably not something I would think the gc would be applied to as much as anything you called by using the new operator, and if such an object, created by new, either goes out of scope leaves { }or is set to null ojectOne = null;// not needed any more. then the static becomes eligible for gc like anything else that no longer is reachable by the program without calling new again.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

If you don't use it again, does the garbage collector will release the memory like non static object? or does it always stay in the memory until the program finishs? If it is running on a server, does it always in the memory and ready to be used until you stop the server?


Whether something is garbage collected is dependent on whether it is reachable -- period. It doesn't matter if it is reachable from static variables of classes, stacks frames from threads of execution, or objects which are reachable. If it is reachable, it will not be garbage collected.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
dennis liang
Greenhorn

Joined: Dec 15, 2006
Posts: 9
Henry,
I know when non static objects are not reachable, but I don't know when static objects are not reachable? I thought they were always there until the program ended.
Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by dennis liang:
Henry,
I know when non static objects are not reachable, but I don't know when static objects are not reachable? I thought they were always there until the program ended.
Thanks


There is no distinction of "non static objects" and "static objects" -- an object is an object. Just because it is referenced by a static variable doesn't change it behavior. When it is no longer reachable, via from both static and non static references, it will be garbage collected.

Henry
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Aside from the fact that the compiler will probably catch this, this is what the typical mistake looks like in general form:



These are hard to catch in a large code project.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Objects referenced by static variables (only) will remain reachable until the class itself is unloaded. In current JVMs a class won't be unloaded until the ClassLoader that loaded the class is GCd. Only classloaders created by the application (for example, custom classloaders are common in application servers) will ever be GCd, and then only if the application itself lets go of them.

So for many applications, the answer is that object referenced by static variables will never be collected. But in something like a Servlet container, when a servlet is reloaded, its classes will be discarded and therefore those objects could be reclaimed.


[Jess in Action][AskingGoodQuestions]
dennis liang
Greenhorn

Joined: Dec 15, 2006
Posts: 9
//first file
public class PoolClass {

private static PoolClass pool = new PoolClass();

public static PoolClass getInstance(){
return pool;
}

private PoolClass() {
//...
}

}

//second file
public class Client{
public static void main(String[] args){
...
...
...
PoolClass poolOne = PoolClass.getInstance();
...
...
...
//could be short or long
...
...
PoolClass poolTwo = PoolClass.getInstance();
}
}

are poolOne and poolTwo the same object or different objects?

Thanks
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282


EFH is more accomplished at this OO stuff so let me let him answer questions you may have about the code I posted here - a rework of your code.


are poolOne and poolTwo the same object or different objects?
Yes.
[ January 18, 2008: Message edited by: Nicholas Jordan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static field