the concept of STATIC variables, and methods in Java

  • A+
Category:Languages

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; }  } 
  1. 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)?

  2. When you make static variables, do you HAVE TO make them in the class? Not possible in the methods?

  3. 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()?

 


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 :

  • instance and static fields at the class top level
  • local variables at the method level

And you use a variable with a specific modifier in any context that is compatible with this modifier :

  • instance variables in an instance context
  • static variables both in instance and static contexts.

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     }   ...  } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: