Skip to content

Instantly share code, notes, and snippets.

@narusemotoki
Last active December 14, 2015 02:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narusemotoki/5011432 to your computer and use it in GitHub Desktop.
Save narusemotoki/5011432 to your computer and use it in GitHub Desktop.
JavaのgetCanonicalName()、getName()、getSimpleName()が外部クラス、外部クラス内匿名クラス、内部クラス、内部クラス内匿名クラスでそれぞれどういう値を返すのかを調べました。
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