8

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.

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 1
    Normally I don't like telling the OP their doing it wrong, but in this case, maybe a little context will help? Defining "many" inner classes usually isn't a good idea. – Jeremy Oct 31 '10 at 15:03
  • Ok. 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. – VansFannel Oct 31 '10 at 15:08
  • 4
    If that's the case, you can define all 10 classes to be package private. That is, instead of `public class Hello`, do `class Hello`. That will make it so only classes within that package can access them. – Jeremy Oct 31 '10 at 15:33
  • Your question embodies a contradiction in terms. An `inner` class is *defined* as a *nested* class that isn't `static.` There is no way to define them other than within another class. If on the other hand you really mean *nested* classes, what you ask is trivial. – user207421 Apr 10 '17 at 12:30

4 Answers4

14

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?

Codemwnci
  • 54,176
  • 10
  • 96
  • 129
  • 1
    Ok. 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. – VansFannel Oct 31 '10 at 15:09
  • You need to contain all your classes inside a package, and make your classes package wide, so they cannot be accessed outside of the package. – Codemwnci Oct 31 '10 at 15:46
9

Put all your classes in a package and define the classes to be package private.

package com.example.here

class Hello{
    //...
}

Notice the absence of the keyword public? You will only be able to create an instance of the class Hello if the class creating it is in the com.example.here package.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
3

Try the following ...

  • Hand over a reference of the outer-class to the no-longer-inner-class
  • Use packages and make the no-longer-inner-class package-private (Jeremy's answer)
  • In the very rarest of cases, it might actually be best to go with inner classes, and at the same time have them do work elsewhere. If this really is you, please read on ...

How to keep inner classes small

a) Extend from outer classes

class Outer {

    class SomeInnerClass extends SomeClass {
        // More specific code here
    }
}

class SomeClass {
    // A lot of generic code here (in a different file)
}

b) Use abstract methods

One of the (more correct) reasons for using inner classes, usually has to do with the use of the exact instance of the outer-class. To tackle it in a generic fashion in the base class, use abstract getters.

abstract class SomeClass {

    protected abstract SpecificData getSpecificData();

    void someMethod() {
        SpecificData specificData = getSpecificData();

        // Do work with the "specific data" here ...
    }
}


class Outer {

    private SpecificData mSpecificData = new SpecificData();

    class SomeInnerClass extends SomeClass {

        @Override
        protected SpecificData getSpecificData() {
            return OuterClass.mSpecificData;
        }
    }
}

I think you get the idea, ... You might also consider using some GeneralData class or interface (within SomeClass) instead, and have getSpecificData() return a more specific (descended-)instance of it.

Again: This can be terribly misused to create very bad unreadable code, but it also can be used for very nice patters under the right circumstances, anyways it should answer the original question.

Levite
  • 17,263
  • 8
  • 50
  • 50
2

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.

If you want to restrict access to a single class, you can put them all in a new package. You will need to move the designated class that is allowed access into this packate, too. For the new classes, you can restrict access by using the default access level (no public/private/protected modifier). This will make them accessible only to the classes in their package. The specified class that is allowed access can be made public so that it can be used outside this new package.

Note: You have the option of restricting the visibility of the class or the visibility of the constructor.

akf
  • 38,619
  • 8
  • 86
  • 96
  • moving packages around is not a good solution, it restrict the use in packages. if the question is for one shoot program, that no one cares about, then it could be a good solution. – none Oct 31 '10 at 15:41