1

I switched from C++ to Java and have a problem with nested classes. I would like to initiate an instance of a subclass in the constructor of the superclass. I tried it like this, but it seems to be wrong.

public class Aircraft {
    public class LandingGear {
    }

    public Aircraft() {
        Aircraft.LandingGear myLandingGear = this.new LandingGear();
    }
}

The idea is that every instance of the class Aircraft has an instance "myLandingGear" of the subclass LandingGear.

9
  • What's wrong with this code? Commented Jan 23, 2017 at 12:39
  • 1
    landing gear should not be a subclass of Aircraft! Commented Jan 23, 2017 at 12:39
  • 1
    There is a misconception of what means has an instance of. This doesn't need to be an inner class. But Aircraft need to have a variable of this class Commented Jan 23, 2017 at 12:40
  • An Aircraft HAS-A LandingGear; an Aircraft is not IS-A LandingGear. It's composition, not inheritance. Parents cannot know about children. Commented Jan 23, 2017 at 12:43
  • Please note that Stack Snippets (the [<>] toolbar button) are for runnable in-browser examples using JavaScript, HTML, and/or CSS, not Java. For Java, just use a simple code block (the {} toolbar button). Commented Jan 23, 2017 at 12:45

3 Answers 3

2

For this use case, your nested class should be static (if it's going to be nested at all), and then you just use a simple new:

public class Aircraft {
    public static class LandingGear {
    // ----^
    }

    public Aircraft() {
        Aircraft.LandingGear myLandingGear = new LandingGear();
    // --------------------------------------^^^^
    }
}

When it's not static, it's an inner class, which from your description isn't what you want. More on nested classes in this Java tutorial.

But unless there's a really good reason for LandingGear to be nested inside Aircraft, consider making it a peer instead.

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

Comments

0

Every Aircraft will have a LandingGear

public class Aircraft {
    private LandingGear myLandingGear;


    public Aircraft() {
        myLandingGear = new LandingGear();
   }

   public LandingGear getLandingGear() {
       return this.myLandingGear;
   }

}

2 Comments

Thank you very much for the quick response, it looks very elegant! Can LandingGear be a class in this case? Is myLandingGear assignable to its Aircraft instance (e.g. myAircraft.myLandingGear)?
Fabisn, yes... LandingGear should be a independent class with his own attributes/methods... When you create a new instance of Aircraft, this arircrat will have an instance of LandingGear as attribute, to gei it use the getter provided ... myAircraft.getLandingGear()
0
public class Aircraft {
    public class LandingGear {

        public static void m1()
        {
         //Your code snipet
        }     

    }

    public Aircraft() {
       LandingGear.m1();
    }
}

1 Comment

Welcome to Stack Overflow! While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.