Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Modifier

 
summer_gsr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic