java - getDeclaredConstructors() lists 2 constructors but there is only one -
given little pice of code:
import java.util.arrays; public class sample { private final int test; private sample(int test) { this.test = test; } public static void main(string[] args) { system.out.println(arrays.tostring(hello.class.getdeclaredconstructors())); } public static class hello { private final int i; private hello(int i) { this.i = i; } public int geti() { return i; } public static class builder { private int i; private builder() { } public static builder builder() { return new builder(); } public void add(int i) { this.i = i; } public hello build() { return new hello(i); } } } }
i don't understand output shown:
[private sample$hello(int), sample$hello(int,sample$1)]
what second listed constructor in here second parameter builder. thought output show private constructor of hello not second one.
builder
static nested class of hello
, therefore should able access private
constructor sample$hello(int)
.
however, jvm doesn't have special support nested classes, therefore, jvm point of view, builder
cannot access sample$hello(int)
.
in order solve problem compiler generates synthetic constructor sample$hello(int,sample$1)
default visibility, , calls instead of sample$hello(int)
builder
.
sample$1
empty class generated compiler make distinguished signature sample$hello(int,sample$1)
. actually, second argument of constructor null
.
Comments
Post a Comment