I really want to know why instead of going to the documentation. Suppose I have class like this:
public class OuterClass{
public static void main(String[] args)throws Exception{
System.out.println(OuterClass.InnerClass.yearOfBorn);//works ok
System.out.println(OuterClass.InnerClass.returnYearOfBorn());//not works
System.out.println(OuterClass.InnerClass.returnYearOfBornAsPublic());//not works
}
private final class InnerClass{
private static final int yearOfBorn=13;
private static int returnYearOfBorn(){
return yearOfBorn;
}
public static int returnYearOfBornAsPublic(){
return yearOfBorn;
}
}
}
Both static method is throwing a error stating that
modifier 'static' is only allowed in constant variable declarations
I know that the docs states that 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).
But my question what is the difference between calling a simple value or property vs calling a static method not matter if is public or private? Why calling a property is possible but not a method?
returnYearOfBornandreturnYearOfBornAsPublicare both invalid members. This code should not compile. But maybe it compiles because you are using a somewhat forgiving compiler (like eclipse's).