-1

I have a question similar to "Defining inner class outside java file". But the questioner, there, just wanted to do something which is possible by letting the classes to be all in a package.

Similar to him I would like to define some classes as the inner objects of an outer object; but I would like to have access to the dynamic fields of that object. To avoid having a crowded classes, I would like to define them in some other "innerClasses.java". Using "extend" only makes me able to use its static fields. But, I would like to access the dynamic fields like an inner object.

As a short example (but in fact a big example would be problematical) I would like this class in the class "OuterClass.java"

public class OuterClass{
    double nonStaticField
}

And each of these classes in other classes

public class InnerClass1OfOuterClass{
    ...
}

public class InnerClass2OfOuterClass{
    ...
}

public class InnerClass3OfOuterClass{
    ...
}

public class InnerClass4OfOuterClass{
    ...
}

....
Community
  • 1
  • 1
hossayni
  • 465
  • 1
  • 3
  • 16
  • 2
    What do you mean by "dynamic fields"? – Oliver Charlesworth Nov 17 '14 at 08:21
  • 1
    It would shock me if this was possible. – wvdz Nov 17 '14 at 08:22
  • Can you outline what the class design would look like? – Fildor Nov 17 '14 at 08:24
  • @OliverCharlesworth non-static fields, I mean – hossayni Nov 17 '14 at 08:29
  • Ok, in your example: Do you want to access `nonStaticField` from one of `InnerClassXOfOuterClass`? Or in `OuterClass` access some field in the `Inner...` ? I don't see why this could not be done by parameter passing and using return value. – Fildor Nov 17 '14 at 08:36
  • @Fildor Exactly because of when you define an inner class. Also in that case it is possible for you to do everything by parameter passing, but you are a child of that object and do not deprive your self from its advantages. – hossayni Nov 17 '14 at 08:40
  • What are those advantages you need so desperately? Fact is, Java will not let you define an inner class outside the definition of the outer class. So we should figure, why you think you need to do this and change your need or your mind :) – Fildor Nov 17 '14 at 08:44
  • 2
    'Inner outside' is a contradiction in terms. This has all the hallmarks of an XY problem. Unclear what you're asking. – user207421 Nov 17 '14 at 08:50
  • @Fildor Several inner classes, as I told, makes the class crowded. I know that it is contradiction in terms. But I wonder (if Java doesn't support this) why they don't provide such useful facility. – hossayni Nov 17 '14 at 08:55
  • @EJP I wrote the reason in the upper comment – hossayni Nov 17 '14 at 08:56
  • "Several inner classes, as I told, makes the class crowded" I am aware of that. My question was why you need so many inner classes? And why do they or the outer class need to access protected fields of each other. – Fildor Nov 17 '14 at 08:57
  • When you're asking for square circles it doesn't matter what your reason is. You've made a logical mistake somewhere. You need to identify it. – user207421 Nov 17 '14 at 09:05
  • @EJP I respectfully disagree. Asking for what the OP **thinks** his reason is may take you on the track of that logical mistake, don't you agree? I am trying to find out what his "Y" is ... – Fildor Nov 17 '14 at 09:09
  • @Fildor Of course, but the OP just repeating his fallacious reasons isn't adequate. – user207421 Nov 17 '14 at 09:22
  • @EJP InnerClass doesn't necessarily means inside the code it means an object inside the other object. So, it is not asking a square to become circle – hossayni Nov 17 '14 at 09:24
  • @Fildor I know the basics of object-oriented programming. Can you present any academic reference which tells that the number of inner classes should be few? – hossayni Nov 17 '14 at 09:26
  • I do not think academic reference is of use here. 1. Java does not allow inner classes to be defined outside of outer class code. 2. If you need many different inner classes the code will be blown up. That's it. – Fildor Nov 17 '14 at 09:28
  • @Fildor Exactly I say that it should be supported to prevent being blown up. If it is not supported, either it has an academic answer (I am a computer scientist) or it maybe the mistake of Java. – hossayni Nov 17 '14 at 09:30
  • Well, you'll have to talk to the Java-People then :) But in the meantime, I personally think it would be better to find alternative designs that avoid the need for that in the first place. It is not really academic (in that I cannot give a reference right now), but I learned that we shall keep classes as small as we can. – Fildor Nov 17 '14 at 09:32

1 Answers1

1

The "natural" way would be the other way around: to let an inner class extend some abstract class containing the major code:

class Outer {
    class Inner extends InnerBase<Outer> {
        ... little code
    }
}

class InnerBase<T> {
    protected int eger;
    ... most code
}

Then there is delegating to a different class too.

Now if you still want to extend Inner:

class InnerBase<T> {
    protected int eger;
    ... most code
}

class Sinner extends Outer.Inner {
}

Outer outer = new Outer();
Inner sinner = outer.new Sinner();

For practical purposes you might create in Outer

protected Inner createInner() { return new Inner(); }

and extend Outer, overriding the factory method createInner.

This code design might be a bit circumstantial, hence my "natural" in the beginning.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I am pretty sure, OP does not need all this at all. I suspect heavily an XY-Problem here. – Fildor Nov 17 '14 at 08:59
  • @Fildor right, this is basically an XY problem: "how can I hammer nails with this screw driver tool?" However exploiting the right language construct (tool) to achieve "elegant" code needs some trying. Here one may assume the OP has code with inner classes and now wants to clean that up. Unfortunately using inheritance. – Joop Eggen Nov 17 '14 at 09:18
  • Extending the outer class is a very good starting point, if you *must* use inner classes for something ... I like it a lot, despite that it could easily be used in wrong context / bad design, the general idea is *very* good! – Levite Apr 06 '17 at 15:15