Skip to content

Instantly share code, notes, and snippets.

@bondolo
Last active June 5, 2018 07:24
Show Gist options
  • Save bondolo/18d651387e8670000354 to your computer and use it in GitHub Desktop.
Save bondolo/18d651387e8670000354 to your computer and use it in GitHub Desktop.
An investigation of inner class behaviour. Prints information about a class and it's enclosing class.
/**
* public domain
*
* https://creativecommons.org/publicdomain/zero/1.0/
*
*/
package test;
import java.lang.reflect.Modifier;
import java.util.concurrent.Callable;
public class Thing implements Callable<String> {
public static String whoami(Class<?> clazz) {
return (Modifier.isStatic(clazz.getModifiers()) ? "static " : " ") + clazz.getName() + " : " + clazz.getEnclosingClass();
}
public final static Callable<String> STATIC_ANON = new Callable<String>() {
@Override
public String call() {
Class<?> clazz = getClass();
return whoami(clazz);
}
};
public final Callable<String> INSTANCE_ANON = new Callable<String>() {
@Override
public String call() {
Class<?> clazz = getClass();
return whoami(clazz);
}
};
public final static Callable<String> STATIC_STATIC = new Static();
public static class Static implements Callable<String> {
@Override
public String call() {
Class<?> clazz = getClass();
return whoami(clazz);
}
};
public final Callable<String> INSTANCE_STATIC = new Static();
public final Callable<String> INSTANCE_INNER = new Inner();
class Inner implements Callable<String> {
@Override
public String call() {
Class<?> clazz = getClass();
return whoami(clazz);
}
};
public final static Callable<String> ASSOCIATED_STATIC = new Associated();
public final Callable<String> INSTANCE_ASSOCIATED = new Associated();
public final static Callable<String> ASSOCIATED_ANON_STATIC = new Associated() {
@Override
public String call() {
Class<?> clazz = getClass();
return whoami(clazz);
}
};
public final Callable<String> INSTANCE_ANON_ASSOCIATED = new Associated() {
@Override
public String call() {
Class<?> clazz = getClass();
return whoami(clazz);
}
};
@Override
public String call() throws Exception {
Callable<String> ANONYMOUS_INSTANCE = new Callable<String>() {
@Override
public String call() {
Class<?> clazz = getClass();
return whoami(clazz);
}
};
return ANONYMOUS_INSTANCE.call();
}
public static void main(String... args) throws Exception {
class Local implements Callable<String> {
@Override
public String call() {
Class<?> clazz = getClass();
return whoami(clazz);
}
}
final Callable<String> LOCAL = new Local();
final Callable<String> ANONYMOUS_STATIC = new Callable<String>() {
@Override
public String call() {
Class<?> clazz = getClass();
return whoami(clazz);
}
};
Thing thing = new Thing();
System.out.println("Thing: " + Thing.whoami(Thing.class));
System.out.println("thing: " + Thing.whoami(thing.getClass()));
System.out.println("STATIC_ANON: " + Thing.STATIC_ANON.call());
System.out.println("INSTANCE_ANON: " + thing.INSTANCE_ANON.call());
System.out.println("INSTANCE_STATIC: " + thing.INSTANCE_STATIC.call());
System.out.println("INSTANCE_INNER: " + thing.INSTANCE_INNER.call());
System.out.println("ASSOCIATED_STATIC: " + Thing.ASSOCIATED_STATIC.call());
System.out.println("ASSOCIATED_ANON_STATIC: " + Thing.ASSOCIATED_ANON_STATIC.call());
System.out.println("INSTANCE_ASSOCIATED: " + thing.INSTANCE_ASSOCIATED.call());
System.out.println("INSTANCE_ANON_ASSOCIATED: " + thing.INSTANCE_ANON_ASSOCIATED.call());
System.out.println("LOCAL: " + LOCAL.call());
System.out.println("ANONYMOUS: " + ANONYMOUS_STATIC.call());
System.out.println("ANONYMOUS_INSTANCE" + thing.call());
}
}
class Associated implements Callable<String> {
@Override
public String call() {
Class<?> clazz = getClass();
return Thing.whoami(clazz);
}
}
@bondolo
Copy link
Author

bondolo commented Jan 16, 2015

Updated with <blush> the most relevant case.

@bondolo
Copy link
Author

bondolo commented Jan 16, 2015

Output of revised version is:

run:
Thing:  test.Thing : null
thing:  test.Thing : null
STATIC_ANON: static test.Thing$1 : class test.Thing
INSTANCE_ANON:  test.Thing$2 : class test.Thing
INSTANCE_STATIC: static test.Thing$Static : class test.Thing
INSTANCE_INNER:  test.Thing$Inner : class test.Thing
ASSOCIATED_STATIC:  test.Associated : null
ASSOCIATED_ANON_STATIC: static test.Thing$3 : class test.Thing
INSTANCE_ASSOCIATED:  test.Associated : null
INSTANCE_ANON_ASSOCIATED:  test.Thing$4 : class test.Thing
LOCAL:  test.Thing$1Local : class test.Thing
ANONYMOUS: static test.Thing$6 : class test.Thing
ANONYMOUS_INSTANCE test.Thing$5 : class test.Thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment