Avatar of Software Software
Software Software
Flag for Austria asked on

Java: Inner classes - How to have access to inner class variables

Hello,
I have a class car and an inner class engine. The inner class shouldn't be visible outside of the class car.
Is there still a way to give a Car an Engine as parameter?
Error: Car.Engine has private access in Car
Here is my code:

public class Main {
    public static void main(String[] args) {
        Car car = new Car("VW", "Red", new Car.Engine("Diesel"));
    }
}

Open in new window


public class Car {
    private String brand;
    private String color;
    private Engine engine;


    public Car(String brand, String color, Engine engine){
        this.brand=brand;
        this.color=color;
        this.engine=engine;
    }

    public Engine getEngineType(){
        return new Engine(engine.getType());
    }


    public void setEngine(Engine engine){
        this.engine=engine;
    }

    private class Engine{
        private String type;

        public Engine(String type){
            this.type=type;
        }

        public String getType() {
            return type;
        }
    }
}

Open in new window

ProgrammingJava

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
girionis

Is there still a way to give a Car an Engine as parameter?
Not normally. You can probably do it with reflection though.
Software Software

ASKER
So, why to use private inner classes? Have someone a good example?
ASKER CERTIFIED SOLUTION
krakatoa

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
dpearson

Like Krakatoa says, if you want to use a private inner class, it should be entirely used for implementation details within the class.

One way to think about this is, you should be able to delete that inner class and replace it with something else and no code outside of the class should be affected.

You can see that the code you tried to write wouldn't satisfy that, because you're building an Engine outside to pass in.  So that code would need to be changed if you deleted Engine.

So even after the rewrite Krakatoa proposed, the class is still passing Engine instances outside itself (e.g. getEngineType()).

I think a better rewrite would be to have getEngineType () return a String (this.engine.getType()) and then the setEngine method could be renamed to setEngineType(String type) and inside that create a new Engine instance.

I'd write out the code, except I'm using my phone and it would take too long to type :(

Anyway, hope that helps,

Doug
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
krakatoa

One way to think about this is, you should be able to delete that inner class and replace it with something else and no code outside of the class should be affected.

You can see that the code you tried to write wouldn't satisfy that, because you're building an Engine outside to pass in.  So that code would need to be changed if you deleted Engine.

So even after the rewrite Krakatoa proposed, the class is still passing Engine instances outside itself (e.g. getEngineType()).

I think a better rewrite would be to have getEngineType () return a String (this.engine.getType()) and then the setEngine method could be renamed to setEngineType(String type) and inside that create a new Engine instance.

This is top advice and deserves more of the credit really on this question. In particular the dimension Doug mentions that inner code should be replaceable without any effect on the outside world. Clincher.

If you want to re-allocate your points in this one Software Software, then do. Inc girionis' contribution. My kludge is a workaround that Doug pointed out is just a workaround.

k.