Last active
December 14, 2015 02:09
-
-
Save narusemotoki/5011432 to your computer and use it in GitHub Desktop.
JavaのgetCanonicalName()、getName()、getSimpleName()が外部クラス、外部クラス内匿名クラス、内部クラス、内部クラス内匿名クラスでそれぞれどういう値を返すのかを調べました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ho.ge; | |
/* | |
結果 | |
Main | |
Canonical: ho.ge.Main | |
Name: ho.ge.Main | |
Simple: Main | |
----- | |
Annoymous | |
Canonical: null | |
Name: ho.ge.Main$1 | |
Simple: | |
----- | |
Inner | |
Canonical: ho.ge.Main.Inner | |
Name: ho.ge.Main$Inner | |
Simple: Inner | |
----- | |
Inner Annoymous | |
Canonical: null | |
Name: ho.ge.Main$Inner$1 | |
Simple: | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
new Main(); | |
} | |
public Main() { | |
System.out.println("Main"); | |
System.out.println("Canonical: " + getClass().getCanonicalName()); | |
System.out.println("Name: " + getClass().getName()); | |
System.out.println("Simple: " + getClass().getSimpleName()); | |
System.out.println("-----"); | |
new Object() { | |
public void method() { | |
System.out.println("Annoymous"); | |
System.out.println("Canonical: " + getClass().getCanonicalName()); | |
System.out.println("Name: " + getClass().getName()); | |
System.out.println("Simple: " + getClass().getSimpleName()); | |
System.out.println("-----"); | |
} | |
}.method(); | |
new Inner(); | |
} | |
class Inner { | |
public Inner() { | |
System.out.println("Inner"); | |
System.out.println("Canonical: " + getClass().getCanonicalName()); | |
System.out.println("Name: " + getClass().getName()); | |
System.out.println("Simple: " + getClass().getSimpleName()); | |
System.out.println("-----"); | |
new Object() { | |
public void method() { | |
System.out.println("Inner Annoymous"); | |
System.out.println("Canonical: " + getClass().getCanonicalName()); | |
System.out.println("Name: " + getClass().getName()); | |
System.out.println("Simple: " + getClass().getSimpleName()); | |
} | |
}.method(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment