I have been searching for clear answers desperately and I think I kinda get it but at the same time I don't quite get the broad concept of that keyword, static.
Here's the scenario I've made:
package oops;
public class Math {
boolean notNumber = false;
static boolean notString = false;
public static void main(String[] args) {
int num1 = 1;
static int num2 = 1; //doesn't work
Math math = new Math();
math.notNumber = true;
notNumber = true; //doesn't work
notString = true;
}
public void whatever() {
notNumber = true;
}
}
Why can't you declare a variable as static inside the static method (or any method)? What does "scope" mean? I know that static variables are not associated with a particular instance of a class, it's more like a "global" variable. But why can't you create a static variable inside the method (num2), but can USE the static variable (notString)?
When you make static variables, do you HAVE TO make them in the class? Not possible in the methods?
Since I declared notNumber as non-static, I know I have to create an object to access that variable. But why does it work in whatever() without any creation of objects, but not in the static method main()?
Static variables belong to the class, with all objects of a class sharing a single static variable. Static methods are associated with the class, not objects of the class. Static variables are used with the class name and the dot operator, since they are associated with a class, not objects of a class.
Storage Area of Static Variable in Java Method area section was used to store static variables of the class, metadata of the class, etc. Whereas, non-static methods and variables were stored in the heap memory. After the java 8 version, static variables are stored in the heap memory.
In Java, static variables are also called class variables. That is, they belong to a class and not a particular instance. As a result, class initialization will initialize static variables. In contrast, a class's instance will initialize the instance variables (non-static variables).
i.e. you cannot use a local variable outside the current method which contradicts with the definition of class/static variable. Therefore, declaring a static variable inside a method makes no sense, if you still try to do so, a compile time error will be generated.
But why can't you create a static variable inside the method (num2), but can USE the static variable (notString)?
When you make static variables, do you HAVE TO make them in the class? Not possible in the methods?
static
is scoped to the class context. So declaring a static
variable in a method scope makes no sense.
It is like if you declared an instance field in a method.
Declaring a variable and using it are two distinct things that don't obey to the same rules.
As a rule of thumb, you declare a variable with a specific modifier in a place that fits to this modifier. For example :
And you use a variable with a specific modifier in any context that is compatible with this modifier :
Since I declared notNumber as non-static, I know I have to create an object to access that variable. But why does it work in whatever() without any creation of objects, but not in the static method main()?
This is an instance method :
public void whatever() {
notNumber = true;
}
So it is valid to access to instance members of the class.
While this other is a static
method. So it can refer static
fields but not instance fields :
public class Math {
boolean notNumber = false; // instance
static boolean notString = false; // static
public static void main(String[] args) {
...
notNumber = true; //doesn't work as refers an instance field
notString = true; // work as refers a static field
}
...
}
Because there is absolutly no need to declare static viriables
in methods.static variables
are members shared by all instances of this class. Suppose it is allowed to declare it in main
method, and you have another static method foo
, then how can you access this variable in foo
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With