Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add Java 1.1 language feature tests: local & anonymous classes, initi…
…alizers, blank finals, class literals
  • Loading branch information
mperor committed Sep 28, 2026
commit 7c1294ed1ce6039c6d3b1acd0a45264e08e8f974
65 changes: 65 additions & 0 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java1.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -69,6 +70,70 @@ public void testInnerAndNestedStaticClasses() {
Assertions.assertEquals("inner secret", inner.getSecret());
}

@Test
public void testLocalAndAnonymousClasses() {
// Local & anonymous classes can capture local variables - they had to be final in Java 1.1, effectively final since Java 8
String greeting = "Hello";

class LocalGreeter {
String greet(String name) {
return greeting + " " + name;
}
}

Object anonymous = new Object() {
@Override
public String toString() {
return greeting + " from anonymous";
}
};

Assertions.assertEquals("Hello John", new LocalGreeter().greet("John"));
Assertions.assertEquals("Hello from anonymous", anonymous.toString());
Assertions.assertTrue(LocalGreeter.class.isLocalClass());
Assertions.assertTrue(anonymous.getClass().isAnonymousClass());
}

@Test
public void testInstanceInitializerAndBlankFinal() {
// Instance initializer blocks run after field initializers (in textual order) and before the constructor body
var initialization = new Initialization(42);
Assertions.assertEquals(List.of("field", "initializer", "constructor"), initialization.steps);

// A blank final is declared without a value and must be assigned exactly once in every constructor
Assertions.assertEquals(42, initialization.id);
}

static class Initialization {

final List<String> steps = new ArrayList<>(List.of("field"));
final int id; // blank final

{
steps.add("initializer");
}

Initialization(int id) {
this.id = id;
steps.add("constructor");
}

}

@Test
public void testClassLiterals() {
// Class literals give the Class object at compile time - no instance or Class.forName lookup needed
Assertions.assertSame(String.class, "text".getClass());
Assertions.assertEquals("java.lang.String", String.class.getName());

// Primitives, arrays & void have their own Class objects, distinct from the wrapper classes
Assertions.assertTrue(int.class.isPrimitive());
Assertions.assertSame(int.class, Integer.TYPE);
Assertions.assertNotSame(int.class, Integer.class);
Assertions.assertSame(void.class, Void.TYPE);
Assertions.assertEquals("[I", int[].class.getName());
}

@Test
public void testJavaBean() throws IntrospectionException {
var bean = new JavaBean(); // public no-arg constructor
Expand Down
Loading