'Can i initialize static variable after the initialization?

What is the problem with below code?

class test {
    static int a;
    a=10;
}

If I'm writing like this (as above), I'm getting a compile time error.

class test { 
    static int a=10;
    a=4;
}

For the second one, I'm not getting any error.



Solution 1:[1]

Neither of your examples should compile.

a=10;

is a statement, which is not valid directly inside a class declaration. You can only put the following directly inside a class:

  • Member declarations (member/static variable declarations (like static int a;), methods, nested classes and interfaces);
  • Static and instance initializers;
  • Constructors.

You need to put a statement inside a block, for example a static initializer:

static int a;

static {
  a = 10;
}

which is equivalent to:

static int a = 10;

Solution 2:[2]

You need to use a static block of statement to do an assignment on an other line (outside a method)

class test {
    static int a;
    static { a=10; }
}

Solution 3:[3]

a=4; must be done in a valid scope

either a method or a constructor...

this line is valid instead

static int a=10;

because java allows youto declare and initialize in one statement!

Solution 4:[4]

If you want to initialize a after defining it as a null int, you can only do that in a function, because it is static.

Solution 5:[5]

must be initialized inside static block or init block or in constructor.
you can only initialized your member variable after declaration inside a function or block because it is static you should use static block

Solution 6:[6]

What you are currently doing is declaring a variable in a class decleration, which is not valid. Looking at this neither of your examples should give you any good result.

In a class declaration you can however initialize a variable:

static int a;

Then if you want to work with it you would have to create a method first (if you are not aware of this I would strongly advise to watch some youtube tutorials or read books about this topic) :

public void foo(int a){ 
a = 6; //Here you can play with your variables and change them
}

In class declararions you can: declarations methods, initializers and constructors. (there is a bit more you can do, however I would have a look at these points before diving in too deep).

Furthermore it seems that you are not aware what a static variable or a static method does, I think the following posts will help you with that:

difference between 'static int' and 'int' in java

What are static method and variables?

I hope I could help and have fun learning Java

Solution 7:[7]

Because for static memory allocated at the time of class loading so we need do like this class test {static int a =10; public static void main(String args[]) { a=12 output(test.a (or) a);}}

Solution 8:[8]

A Java class body is should contain declarations and declaration with an initializer, not the standalone statements. Let's unwrap your requirement of initializing a static variable.

Static variables should not be initialized inside methods as these variables belong to the class. But they can be initiated inside the constructor.

public class Example {
   static String name;
   static int age;
   Example() {
      name = "James";
      age = 34;
   }
   public static void main(String args[]) {
      new Example(); 
      System.out.println(Example.name); 
      System.out.println(Example.age);
   }
}

Now the output will be :

James
34

If new Example(); is not called then the output will be :

null 
0

This is because the static variables are initialized with an object creation and for each Example object name and age will be overwritten with the same value. But at least one object needs to be created for name and age to be initialized.

But if you declare a static and final instance variable, we cannot initialize that in the constructor, it is mandatory to initialize static and final variables at the class level. We can initialize while declaring the variable or using a static initialization block. This runs before the main() method in Java. Hence when the main() method is loaded this static variable will be initialized and the compiler will not throw any errors.

  static final int name = "STRING";

  static final int age;
  static {
    age = 10;
  }

Putting all these things together,

  • A static variable can be initialized,

    • When declaring.
    • Inside a constructor.
    • Inside a static block.
  • A static final variable can be initialized,

    • When declaring.
    • Inside a static block.
  • A final variable can be initialized,

    • When declaring.
    • Inside a constructor (If you have more than one constructor in your class then it must be initialized in all of them).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 AxelH
Solution 3 ΦXocę 웃 Пepeúpa ツ
Solution 4 user1734984
Solution 5 surajy79
Solution 6 Community
Solution 7 Attri
Solution 8 Sarangan