This is my first class:
package trickycorejava; public class InnerClass { int id; oneClass oneClass; public int getId() { return id; } public void setId(int id) { this.id = id; } public trickycorejava.oneClass getOneClass() { return oneClass; } public void setOneClass(trickycorejava.oneClass oneClass) { this.oneClass = oneClass; } public InnerClass(int id, trickycorejava.oneClass oneClass) { this.id = id; this.oneClass = oneClass; } public InnerClass(int id){ this.id = id; } } class oneClass { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } This is the class where the main method exists, observe that the package is different:
package trickycorejava.constructor; import trickycorejava.InnerClass; public class InnerClassTest { public static void main(String[] args) { InnerClass innerClass = new InnerClass(1); } } How can I initialize the InnerClass with constructor in this case? If I use
InnerClass innerClass = new InnerClass(1, new oneClass("Test"));
I get the error that oneClass is not public cannot be access from outside package.
oneClassin its own fileoneClass.java(in the same package/folder asInnerClass.java) and add the modifierpublicbefore the keywordclass. A remark on your format: class names should always start with an uppercase character:oneClass->OneClassotuerclass? Seems quite inconvenient.publicsince only one class per file is allowed to bepublic, and this class must have the same name as the file it resides in.