Modifier Static Is Only Allowed in Constant Variable Declarations

Modifier static is only allowed in constant variable declarations

You can make the Control class static.

private static class Control {
^^^^^^

// Ok to have static members:
public static ArrayList<String> keys = new ArrayList<String>();

...

This is described in the Java Language Specification Section §8.1.3

8.1.3 Inner Classes and Enclosing Instances


An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (§15.28).

modifier 'static' is only allowed in constant variable declarations?

Your brackets are wrong. Add one at the end of your Cat class and remove the last one

What I'm getting an error 'modifier 'static' is only allowed in constant variable declarations'?

The tester class should contain the main method along with other static classes and methods.

public class StudentTester{
public static void main(String[] args){
student S1 = new student();
student S2 = new student();
S1.setGrade(12);
}

public static class student {

private String name;
private int age;
private int grade;
private double average;
private boolean disability;

private void printStudentInfo(){
//Data Encapsulation is methods of the public interface provide access to private data, while hiding implementation.
System.out.println("Name: "+name+",Age: "+age+",Grade: "+grade+",Average: "+average +" Disability: "+disability);
}

public void setGrade(int newGrade){
grade=newGrade;
}

public int getGrade(){
return grade;
}
}
}

Also check, Static Classes In Java

Illegal static declaration in inner class in jsp scriplet

Modifier static is only allowed in constant variable declarations. you can not define static method in inner class. You can not Use :

 public static void main(String[] args) throws IOException 

In your inner class.

For more details see:
Modifier static is only allowed in constant variable declarations.

Final, immutable objects are not constants?

You can find the reason behind this in the JLS.

8.1.3. Inner Classes and Enclosing Instances

It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable (§4.12.4).

Then, we can check the definition of a constant variable :

4.12.4. final Variables

A constant variable is a final variable of primitive type or type String that is initialized with a constant expression

This is why you are allowed to declare a primitive or a String constant. But the Integer class and other boxing class are not part of that exception, those are instance like any other class.

Source : Andy Thomas

Inline constant

If we add that with the following :

13.1. The Form of a Binary

A reference to a field that is a constant variable (§4.12.4) must be resolved at compile time to the value V denoted by the constant variable's initializer.

We can see that those constant don't really exist at run time, the references are resolved at compile time.

Code :

final static int INTEGER_CONSTANT = 1;
int value = INTEGER_CONSTANT;

Run time "code" :

int value = 1;


Related Topics



Leave a reply



Submit