5

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

Ke Ke
  • 63
  • 1
  • 7
  • How would a local static variable work? What would its special properties be for being “static”? – Edwin Dalorzo Jul 07 '18 at 14:08
  • 2
    Possible duplicate of [What does the 'static' keyword do in a class?](https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) – juzraai Jul 07 '18 at 14:09
  • Thanks for the link, that definitely helped me understand it better – Ke Ke Jul 07 '18 at 14:55

5 Answers5

2

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?

xingbin
  • 27,410
  • 9
  • 53
  • 103
2

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
    }
  ...
 }
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thanks for your thoughtful answer, may I humbly ask what an "instance field" is like you mentioned in your answer, "It is like if you declared an instance field in a method." Isn't instance = object? So basically non-static? – Ke Ke Jul 07 '18 at 15:04
  • You are welcome. That is indeed an object. In your actual code : `boolean notNumber = false; ` is an instance field. And each instance of a class has its own state in terms of instance fields. – davidxxx Jul 07 '18 at 15:16
1
  1. You seem to understand that static members don't belong to any instance. They belong to the class itself. They are not really "global" like you have said. You still need to write the class name first when you want to access them from another class. Because static members belongs to the class, the only sensible place for them to exist is at the class-level. You can't declare them in methods because it would make them seem like they belong to methods (as in, their scope is the method body), which they don't. You can use them in methods of the same class without qualifying the class name though, because the method is in scope of the class.

  2. They have to be in the class, as mentioned above.

  3. This is a good question. If you look carefully, whatever is not static either! That means you have to have an instance first, before whatever can be executed. This instance, in whatever, is referred to as "this". Your use of notNumber is short for this.notNumber. You are using the notNumber field of the instance on which whatever is called.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thanks for your thoughtful answer. Just to clarify my thoughts, you said, "You still need to write the class name first when you want to access them from another class. " Is it because it's static? you just go className.variable, like that? cuz if it's non-static, you would have to create an object and go like this, objectName.variable. Is that correct? – Ke Ke Jul 07 '18 at 14:41
  • @KeKe Yes! If you think my answer answers your question, please consider accepting it by clicking on that checkmark! – Sweeper Jul 07 '18 at 15:01
1

Let us answer it one by one. But before that let us understand what is static variable and what is non-static variable. The static variable is a variable that belongs to a Class and not to any specific instance of the class whereas on the other hand a non static variable belongs to a class instance.

So consider below example

class A{
static Object someStaticValue;
Object someNonStaticValue;
}

A.someStaticValue = new Object(); //allowed
A.someNonStaticValue = new Object() //not allowed as someNonStaticValue belongs to instance of A and will be different for each instance of A
A objectA = new A();
objectA.someNonStaticValue = new Object(); //allowed as this will update someNonStaticValue which is in scope of objectA

objectA.someStaticValue = new Object(); //allowed as it will simply update the value of variable in Class scope.

A objectB = new A();
objectB.someNonStaticValue = new Object(); //allowed
objectB.someStaticValue = new Object(); //allowed as it will simply update the value of variable in Class scope.

objectA.someNonStaticValue is not equal to objectB.someNonStaticValue as they belong to two different scope

whereas A.someStaticValue, objectA.someStaticValue, objectB.someStaticValue -> All are equal as they are updating the variable in class scope.

Now coming to your questions:

Why can't you declare a static variable in a static or non-static method?

Any variable declared inside a method will have scope inside a method and hence can never belong to a class. Every time you will call that method a new instance will be used and hence will be different for each call and hence no purpose of making a variable static inside a method

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

Yes, because creating in methods will solve no purpose and will not be able to keep the definition of static as the values will change in different calls to that method.

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

Because in whatever() function, it is sure that you have created a new instance of the class using which you have called this function, calling a non-static variable inside a non-static variable is allowed. Also, in this case, its clear to JVM which instance scope to use for this variable. However, when it comes to static method, there is no guarantee that the instance of the class has been created or not and also of which class instance scope to be used for that non-static variable. Hence, calling non-static variable from a static method is not allowed.

Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
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)?

ANSWER: You can not declare varibale as static inside a method. Inside method all variables are local variables that has no existance outside this method thats why they cann't be static.

But why does it work in whatever()?

ANSWER: because boolean notNumber is declared on class level, and whatsoever method is non static that's why. you cannot reference non static in static

WHY YOU CANNOT REFERENCE NON STATIC IN STATIC? Why non-static variable cannot be reference from a static context - reg

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

ANSWER you cannot make static variables inside static method. SEE FIRST ANSWER

AshishUpadhyay
  • 179
  • 1
  • 15