0

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.

5
  • Move oneClass in its own file oneClass.java (in the same package/folder as InnerClass.java) and add the modifier public before the keyword class. A remark on your format: class names should always start with an uppercase character: oneClass -> OneClass Commented Jun 20, 2019 at 20:08
  • @Turing85, That is what I am doing but is there not another option to initilize the otuerclass? Seems quite inconvenient. Commented Jun 20, 2019 at 20:12
  • The "inner class" (which is not really inner, just an additional class in a file) has default visibility and is thus not visible from other packages. You cannot set the visibility of the second class to public since only one class per file is allowed to be public, and this class must have the same name as the file it resides in. Commented Jun 20, 2019 at 20:14
  • @Turing85, Even protected is not allowed. Commented Jun 20, 2019 at 20:33
  • Well... protected does not make sense in this context =) how would you derive from a class that is in another package, if you cannot see it? Commented Jun 20, 2019 at 20:38

1 Answer 1

2

As Turing85 pointed out the oneClass should be in it's own file, otherwise it's going to be package-private which means you can only access it from classes of the same package.

Is there another way? There is, but it's not going to be a simple constructor call. Using reflection you can bypass class, field and method invocation protection.

public class InnerClassTest { public static void main(String[] args) throws Exception { Constructor<OneClass> constructor = OneClass.class.getDeclaredConstructor(String.class); constructor.setAccessible(true); OneClass instance = constructor.newInstance("John"); InnerClass innerClass = new InnerClass(1, instance); } } 

What this does is that it finds the constructor that is private to Main because the class is package-private. Then it disables the protection of it, note that these are temporary, the Constructor object is a new reference and only allows the invocation via this reference.

But I don't recommend doing this extensively. Reflection has some use cases, mainly to aid programmers in frameworks like Spring, but otherwise it can break object oriented patterns.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.