1

The compiler says "illegal modifier for parameter i". Please tell me what I'm doing wrong. Why can't I declare a static variable in a Java constructor?

class Student5{  
  
    Student5() {  
        static int i = 0;
        System.out.println(i++);  
    }

    public static void main(String args[]){  
        Student5 c1 = new Student5();
        Student5 c2 = new Student5();
        Student5 c3 = new Student5();
    }
}  
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Deepak Verma
  • 33
  • 1
  • 1
  • 3
  • Please format your code and question properly. – Turing85 Jul 19 '15 at 17:49
  • 1
    possible duplicate of [Initialize a static final field in the constructor](http://stackoverflow.com/questions/5093744/initialize-a-static-final-field-in-the-constructor) – Tot Zam Jul 19 '15 at 17:51

4 Answers4

8

Because of where you are declaring i:

Student5(){  
    static int i=0;
    System.out.println(i++);  
}

the compiler treats it as a local variable in the constructor: Local variables cannot be declared as static. For details on what modifiers are allowed for local variables, see Section 14.4 of the Java Language Specification.

Judging from what the code appears to be trying to do, you probably want i to be a static member of Student5, not a local variable in the constructor:

class Student5{
    private static int i = 0;

    Student5(){  
        System.out.println(i++);  
    }

. . .
}  
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
2

If you want to declare static variable then declare it outside of the constructor, at class level like this -

public class Student5{

   private static int i;

}  

You declaration of static occurred at your constructor which is a local variable and local variable can not be static. And that's why you are getting - illegal modifier for parameter i. And finally for initializing static variable you may use a static initialization block (though it's not mandatory) -

public class Student5{

   private static int i;

   static {
      i = 5;
   }

}  
Razib
  • 10,965
  • 11
  • 53
  • 80
0

This is how the language was designed.. What if you wanted to have another int field named i in the constructor?, then which i should be considered?. Also, static fields are initialized before the constructor is called i.e, during class initilization phase. A constructor gets called only when a new instance is created.

Imagine what would happen (supposed to happen) if you load and initialize a class but not create a new instance.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Static variables are variables that can be referenced without having an instance of the class. By defining one instead of a constructor, which is called when you create an instance of the class, you are contradicting yourself. Either make it defined without having an instance (outside of the constructor and static) or make it specific to an instance (inside the constructor and not static).

You might want to rethink what you are actually trying to do and if you really need a static variable.

Zarwan
  • 5,537
  • 4
  • 30
  • 48