Inner
is private, it can be accessed only from within Main
. Even if a
is public, but Inner
is private and cannot be accessed from a particular place, attribute of a
would be irrelevant.
class Main {
private static class Inner {
public static int a = 1;
}
public static void main(String[] args) {
System.out.print(Inner.a);
}
}
When you run ,
1
Whenever Inner
is static in the same class,(Main
), Inner
(and all its members) are always accessible from anywhere in the Main
class. It is private outside of Main
Class. If I were to access
Main main = new Main();
main.Inner; // Compilation Failure. Inner is not accessible
// Technically I would never do new Main(), but to illustrate I am doing so.
Or a more sensible example of the same
class Main {
public static void main(String[] args) {
System.out.print(SomeClass.Inner);
}
}
class SomeClass{
private static class Inner {
public static int a = 1;
}
}
To illustrate the difference of making a
private or public, consider the following two examples
class Main {
public static void main(String[] args) {
System.out.print(SomeClass.Inner.a);
}
}
class SomeClass{
public static class Inner {
private static int a = 1; // If a is private, it produces an error above
//public static int a = 1; // This would not be an error
}
}
So in your particular case, as long as it is in same class Main
, it is irrelevant, but if it is outside as next example, it would matter.