File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Static Modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Modifier" Watch "Static Modifier" New topic

Static Modifier


Joined: Jan 12, 2001
Posts: 7
Iam learning java and I confused with the concept of static modifier. I was hoping if anyone can clarify my doubt.
class StaticTest
static int y;
int x;
static void method()
static int a=10;
int b=30;
System.out.println("a "+a+"b "+b);
System.out.println("y "+y+"x "+x);
This is giving me a compilation error because I defined a static variable in a static method. Why cann't I declare a static variable in a static method.

Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
You probably are coming from C/C++ programming!
In Java the static modifier means that there is one item for the class versus one item for every instance. In C/C++ using a static modifier inside a method would make the variable retain its value between method invokations. In Java all values only have scope within their program block.
Taking your example we can see that no matter how many times we call: new StaticTest(), we will only have one variable "y" that all instances use (only one memory location used). However every instance created will have its own "x" variable (many memory locations used).
In your case, you can use the variable y to retain its value for all the method calls. Since any object used to invoke the method will be pointing to the same memory location. If that is what you were trying to do in the first place ...
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
The variables inside the method are local variables. As such they only live as long as the method is executing. Before the method is invoked they do not exist. Each time the method is invoked they are created again as distinct variables.
To name one of them as static is to say that it is created at class load time, and there is only one occurance of it (not one for each instance). Those two concepts are in conflict - so the compiler complains.

You also have a problem that you are referencing x, which is a non-static variable in a static method.
Since you can use a static method without ever creating an instance of the class, it would be difficult print what the value of x is without naming the particular object that you want to print the x of.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

It is not the static method that is giving you the problem... You cannot use the static modifier on any local variables (i.e. variables defined inside a method.) This is because a variable defined as static exists on the class level rather than the object level. Basically, what this means is that if you create a class of the following type -

Every time you create a Foo object, they will share the same x variable. For example, if you have the code -

This will set x for both Foo a and Foo b to 1... think of static as a variable that is shared among all the objects you create of that class.
Static variables cannot be inside a method ( static or otherwise ) because local variables ( variables inside a method ) only exist inside that method... they should not hang around after that method has finished. Static variables basically always exist, so you cannot create something that will always exist inside a method who must let go of all its resources once it has ended.
Hope this helps,
- Nate

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Static Modifier
It's not a secret anymore!