Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static question..

Status
Not open for further replies.

patnim17

Programmer
Jun 19, 2005
111
US
Hi,
If I have a Static references to my objects (Weblogic), then these references are stored in the heap I believe. So does this mean that there references are available for the entire time the machine is on, even if the JVM (Weblogic) is down?And when the JVM is up again, will it have access to these references...

can someone put some light on this..or confirm if this is correct.

thanks
pat
 
A static class variable means that, in the JVM process, there is only one version of this variable - so no matter how many instances of a class you have, each instance will always reference the same variable.

Obviously, if the JVM dies, then the value of the static variable will no longer exist, nor be persisted, because it is resident in the memory allocated to the JVM.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks Sedj for the response. Got a question regarding the same.

If I have 2 java class, say classA and classB and both have a class level static reference lik this:
private static MyManager objManager = null;

Now during run time , if classA initializes objManager, now does that mean ii will be available for classB also...Iam guessing not..because both references different static memory location...right??

pat

 
No, that will result in two different MyManager classes.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Would be the same manager for two isntances of the same class.

Cheers,
Dian
 
I'm not sure it would Dian.

Consider this code :

Code:
class ClassA {
	public static ClassC c = new ClassC();
}

class ClassB {
	public static ClassC c = new ClassC();
}

class ClassC {
	public ClassC() {
		System.out.println(this);
	}
}

public class ClassD {
	public static void main(String args[]) {
		new ClassA();
		new ClassB();
	}
}

My output :

java -cp . ClassD
ClassC@360be0
ClassC@1372a1a

Which shows two different instances of the static ClassC object.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
For sure it won't that way.

I meant if classA and classB were objects and not classes, instances of the same class.

I made the aclaration because I've seen some other posts where classes and objects where mixed, just wanted to make sure the question was properly written.

Cheers,
Dian

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top