Skip to content

Instantly share code, notes, and snippets.

@obfusk
Last active November 1, 2024 17:36
Show Gist options
  • Save obfusk/83822140509dad4148b14bba41adf008 to your computer and use it in GitHub Desktop.
Save obfusk/83822140509dad4148b14bba41adf008 to your computer and use it in GitHub Desktop.
$ java Test.java
args=
Bar initialiser...
Bar()foo=Test$Foo@419c5f1a
$ java Test.java optimised
args=optimised
Bar initialiser...
Bar()foo=Test$Foo@419c5f1a
$ java Test.java nosuper
args=nosuper
Bar initialiser...
tcns.foo=Test$Foo@419c5f1a
Bar()foo=null
$ java Test.java nosuper optimised
args=nosuper,optimised
Bar initialiser...
tcns.foo=Test$Foo@419c5f1a
Bar()foo=Test$Foo@419c5f1a
import java.util.Arrays;
import java.util.List;
class Test {
public static TestClassNoSuper tcns;
public static TestClassExtendsBar tceb;
public static class Foo {}
public static class Bar {
static {
System.out.println("Bar initialiser...");
if (tcns != null) {
System.out.println("tcns.foo=" + tcns.foo);
tcns.foo = null;
}
if (tceb != null) {
System.out.println("tceb.foo=" + tceb.foo);
tceb.foo = null;
}
}
public Bar() {}
public Bar(Foo foo) {
System.out.println("Bar()foo=" + foo);
}
}
public static class TestClassExtendsBar extends Bar {
protected Foo foo;
public void useFoo() {
this.foo = new Foo();
// System.out.println("...");
new Bar(this.foo);
}
public void useFooOptimised() {
final Foo _foo = new Foo();
this.foo = _foo;
// System.out.println("...");
new Bar(_foo);
}
}
public static class TestClassNoSuper {
protected Foo foo;
public void useFoo() {
this.foo = new Foo();
// System.out.println("...");
new Bar(this.foo);
}
public void useFooOptimised() {
final Foo _foo = new Foo();
this.foo = _foo;
// System.out.println("...");
new Bar(_foo);
}
}
public static void main(String[] args) {
System.out.println("args=" + String.join(",", args));
List<String> largs = Arrays.asList(args);
if (largs.contains("nosuper")) {
// System.out.println("--");
tcns = new TestClassNoSuper();
// System.out.println("--");
if (largs.contains("optimised")) {
tcns.useFooOptimised();
} else {
tcns.useFoo();
}
// System.out.println("--");
} else {
// System.out.println("--");
tceb = new TestClassExtendsBar();
// System.out.println("--");
if (largs.contains("optimised")) {
tceb.useFooOptimised();
} else {
tceb.useFoo();
}
// System.out.println("--");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment