-
Bug
-
Resolution: Not an Issue
-
P4
-
21
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
I noticed that I can nest in a class 2 records with the same name. When instantiating such a record, the second record class is instantiated. The compiler will however reject the declaration of a 3rd nested record with the same name (with a "Duplicate class" error).
https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.5 mentions name hiding related to inheritance from superclasses/superinterfaces, but not within the same actual class. It also states "However, any attempt within the body of the class to refer to any such member class or interface by its simple name will result in a compile-time error, because the reference is ambiguous.", but this is apparently not true in this case.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See the example class in the "Source code for an executable test case"
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compiler error "Duplicate class: Person" on the second "Person" record
ACTUAL -
Class can be compiled and executed, result is:
Pеrson#2: foo
Pеrson#2: bar
---------- BEGIN SOURCE ----------
public class NameCollisionTest {
record Pеrson(String namе) {
@Override
public String toString() {
return "Pеrson#1: " + namе;
}
}
static {
System.out.println(new Person("foo"));
}
record Person(String name) {
@Override
public String toString() {
return "Pеrson#2: " + name;
}
}
public static void main(String[] args) {
System.out.println(new Person("bar"));
}
}
---------- END SOURCE ----------
FREQUENCY : always
I noticed that I can nest in a class 2 records with the same name. When instantiating such a record, the second record class is instantiated. The compiler will however reject the declaration of a 3rd nested record with the same name (with a "Duplicate class" error).
https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.5 mentions name hiding related to inheritance from superclasses/superinterfaces, but not within the same actual class. It also states "However, any attempt within the body of the class to refer to any such member class or interface by its simple name will result in a compile-time error, because the reference is ambiguous.", but this is apparently not true in this case.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See the example class in the "Source code for an executable test case"
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compiler error "Duplicate class: Person" on the second "Person" record
ACTUAL -
Class can be compiled and executed, result is:
Pеrson#2: foo
Pеrson#2: bar
---------- BEGIN SOURCE ----------
public class NameCollisionTest {
record Pеrson(String namе) {
@Override
public String toString() {
return "Pеrson#1: " + namе;
}
}
static {
System.out.println(new Person("foo"));
}
record Person(String name) {
@Override
public String toString() {
return "Pеrson#2: " + name;
}
}
public static void main(String[] args) {
System.out.println(new Person("bar"));
}
}
---------- END SOURCE ----------
FREQUENCY : always