aspose file tools*
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
Author

Static Modifier

summer_gsr
Greenhorn

Joined: Jan 12, 2001
Posts: 7
Hi,
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.
Summer


GSR
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi,
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 ...
Manfred.
Cindy Glass
"The Hood"
Sheriff

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
Bartender

Joined: Oct 18, 2000
Posts: 4121

Summer,
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


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Modifier