Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining inner class outside java file

I want to create a class, ClassB, as inner class of ClassA, but I want to write down outside ClassA.java file.

How can I do this?

It will be a lot of inner class, and ClassA.java file will be enormous.

UPDATE
What I really want to do is define ten classes that they will be only accessible by one class. All of them are defined inside the same package.

Thanks.

like image 815
VansFannel Avatar asked Oct 31 '10 14:10

VansFannel


People also ask

How do you create an inner class instance from outside the outer class instance code?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

Can an inner class be accessed from outside package?

Inner Class You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.

Can outer Java classes access inner class?

If the inner class defined as private and protected, can outer class access the members of inner class? Yes.

Can inner class be public?

Java inner class can be declared private, public, protected, or with default access whereas an outer class can have only public or default access.


1 Answers

The simple answer, is no you cannot.

By virtue of being an inner class, the class has to be inside the scope of the parent class.

If your class is really going to be enormous, it probably says something about the design of your class. Are you making proper use of encapsulation?

like image 162
Codemwnci Avatar answered Oct 29 '22 00:10

Codemwnci