All top-level classes are, by definition, static.
What the static boils down to is that an instance of the class can stand on its own. Or, the other way around: a non-static inner class (= instance inner class) cannot exist without an instance of the outer class. Since a top-level class does not have an outer class, it can't be anything but static.
Because all top-level classes are static, having the static keyword in a top-level class definition is pointless.
Some code to play around with:
public class Foo {
public class Bar {
// Non-static innner class
}
public static class Baz {
// Static inner class
}
}
public class Example {
public static void main(String[] args) {
new Foo(); // this is ok
new Foo.Baz(); // this is ok
new Foo.Bar(); // does not compile!
Foo f = new Foo();
Foo.Bar bar = f.new Bar(); //this works, but don't do this
}
}
I put the "but don't do this" in there because it's really ugly code design. Instance inner classes should not be visible outside the outer class. They should only be used from within the outer class.
Inner class는 그 안에서만 사용. 그러니까 inner로 만들었자나아아아
Top level class는 static 사용 못함.
'Java' 카테고리의 다른 글
오버로딩(Overloading) / 오버라이딩(Overriding) (0) | 2019.08.30 |
---|---|
[Java] Collection framework (0) | 2019.08.29 |
[Java] 추상 클래스(abstract class)와 인터페이스(interface) (0) | 2019.08.29 |
[Java] 상속 (inheritance) 정의 / final 클래스와 메소드 / instanceof 연산자 (0) | 2019.08.28 |
[Java] 클래스: 구성 멤버 / final / 접근제한자(access modifier) (0) | 2019.08.27 |