Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the concept of STATIC variables, and methods in Java [closed]

Tags:

java

scope

static

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

like image 899
Ke Ke Avatar asked Jul 07 '18 14:07

Ke Ke


People also ask

What are static variables and methods in Java?

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.

Where static variables and methods are stored in Java?

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.

What are the static variables in Java?

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

Can static variables be declared in a method in Java?

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.


2 Answers

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
    }
  ...
 }
like image 65
davidxxx Avatar answered Nov 06 '22 10:11

davidxxx


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?

like image 39
xingbin Avatar answered Nov 06 '22 09:11

xingbin