Home arrow static arrow Java Programming [Archive] - static class variable
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - static class variable
This topic has 6 replies on 1 page.

Posts:56
Registered: 9/12/99
static class variable  
Aug 7, 2004 10:02 AM



 
Folks:

I am little confused by static class variables in Java. Since Java doesn't have global varaibles it uses static variables instead. Please take a look at the following code and please tell me what goes wrong.

/********** CONTENT OF Stack.java ***********/
import java.util.Stack;

public class StackClass
{ static Stack stack = new Stack (); }

/********** CONTENT OF Test1 .java ***********/
public class Test1
{
public static void main( String[] args )
{
StackClass.stack.push("Hello World");
}
}

/********** CONTENT OF Test2.java ***********/
public class Test2
{
public static void main( String[] args )
{
System.out.println( "Top on stack is " + StackClass.stack.peek() );
}

I execute the above programs in the sequence of StackClass.java, Test1.java and Test2.java. I think in Test1.java after I push one element to the stack it should still be in the stack in Test2.java But I got :

java.util.EmptyStackException
at java.util.Stack.peek(Unknown Source)
at Test2.main(Test2.java:16)
Exception in thread "main"

Can anybody give me a hint?

Thanks a lot !

 

Posts:8,813
Registered: 10/4/00
Re: static class variable  
Aug 7, 2004 10:10 AM (reply 1 of 6)



 
After you run StackClass, the JVM (java.exe) ends and all the classes are unloaded including StackClass

When you run Test1, StackClass is loaded, an item is pushed on the stack, and then the JVM exits and all classes are unloaded including StackClass

When you run Test2, StackClass is loaded, and you get an error because the StackClass which was just loaded has no items in it.
 

Posts:56
Registered: 9/12/99
Re: static class variable  
Aug 7, 2004 10:12 AM (reply 2 of 6)



 
Thanks for the resonse which makes a lot of sense. So how can I make that static global ?
 

Posts:3,081
Registered: 2/15/99
Re: static class variable  
Aug 7, 2004 10:15 AM (reply 3 of 6)



 
You mean store a value in one program, then later access the data from another program?

Write the data into a file or a database.
 

Posts:8,813
Registered: 10/4/00
Re: static class variable  
Aug 7, 2004 10:15 AM (reply 4 of 6)



 
You have to write it to persistant storage. A file perhaps.
 

Posts:18,384
Registered: 21.03.00
Re: static class variable  
Aug 7, 2004 10:21 AM (reply 5 of 6)



 
Hi,

That wouldn't work in any language since the program terminates, so a global variable wouldn't help you. If you want to store the value between executions, then look at the preferences class.

http://java.sun.com/j2se/1.4.2/docs/api/index.html

/Kaj
 

Posts:27,518
Registered: 11/3/97
Re: static class variable  
Aug 7, 2004 3:53 PM (reply 6 of 6)



 
I am little confused by static class variables in
Java. Since Java doesn't have global varaibles it uses
static variables instead.

That reasoning is not correct.

C++ has global variables but classes have static member variables as well.

The intent of statics is for all instances of the class to have access to it.
 
This topic has 6 replies on 1 page.