Inner and Nested classes
package com.company.bean;
public class Kia {
public static class A { // nested class or static class
public static void foo() {
System.out.println("A");
}
}
public class B { // inner class or instance class
public void foo() {
System.out.println("B");
}
}
}
package com.company;
import com.company.bean.Kia;
public class Main {
public static void main(String[] args) {
Kia.A a = new Kia.A();
Kia.B b = new Kia().new B();
a.foo();
b.foo();
Kia.A.foo();
System.out.println(a.getClass().getName());
System.out.println(b.getClass().getName());
}
}Outcome:
A
B
A
com.company.bean.Kia$A
com.company.bean.Kia$B
As we can see nested classes must be static, however inner classes instance.
And another important fact is there are anonymous(inner classes after compiling.
Комментарии
Отправить комментарий