troubleshooting Question

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

Avatar of Software Software
Software Software asked on
5 Comments1 Solution116 ViewsLast Modified:
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"));

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

    public Car(String brand, String color, Engine engine){

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

    public void setEngine(Engine engine){

    private class Engine{
        private String type;

        public Engine(String type){

        public String getType() {
            return type;

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros