bill
bill

Reputation: 368

Creating Inner Classes and using them (understanding some code)

I have been reading through some documentation on inner classes, and am currently reading my textbook, where I have found some good knowledge on using Inner classes. For starters I would like to consider the following example and just make sure I am understand things correctly. For reference I have read the follwing Inner Classes Documentation and have read a couple of SO questions. Hopefully someone can guide me through the following.

import java.util.ArrayList;
public class Gearbox {
    private ArrayList<Gear> gears;
    private int maxGears;
    private int currentGear;

    public Gearbox(int maxGears)
    {
        this.maxGears = maxGears;
        this.gears = new ArrayList<Gear>();
        Gear neutral = new Gear(0,0.0);
        this.gears.add(neutral);
    }

    public class Gear
    {
        private int gearNumber;
        private double ratio;

        public Gear(int gearNumber,double ratio)
        {
            this.gearNumber = gearNumber;
            this.ratio = ratio;
        }

        public double driveSpeed(int revs){
            return revs * ratio;
        }
    }
}

In the main.java

public class Main {
    public static void main(String [] args)
    {
       Gearbox ford = new Gearbox(6);
        Gearbox.Gear first = ford.new Gear(1,20);
        System.out.println(first.driveSpeed(10));
    }
}

What I am kind of confused about is the notation to actually use some stuff from the nested class. For example

 Gearbox ford = new Gearbox(6);

Here we make a ford object belonging to the class of Gearbox, which passes in a value of 6 to maxGears.

Next we have Gearbox.Gear first = ford.new Gear(1,20);, Here is where I am kind of confused on what is really going on. Does the .Gear let the compiler know that Gear is an inner class within Gearbox? If so, why is the next statement ford.new Gear(1,12.3)?

Wouldn't something like Gearbox.Gear first = new ford.Gear(1,12.3); make more sense?

Upvotes: 1

Views: 87

Answers (2)

Konstantin Labun
Konstantin Labun

Reputation: 3898

Your inner class is not static, which means it belongs to a concrete instance.

Gearbox.Gear first = new ford.Gear(1,12.3);

Means that you create a Gear for instance ford.

Here you can read some more about inner class and about difference of static and non-static inner classes.

Upvotes: 2

Stephen C
Stephen C

Reputation: 718758

Next we have

Gearbox.Gear first = ford.new Gear(1,20);

Here is where I am kind of confused on what is really going on. Does the .Gear let the compiler know that Gear is an inner class within Gearbox?

Yes ... sort of. Actually, it is saying "I am talking about the Gear class that is declared in Gearbox. It is not necessarily an inner class: it could be a nested class.

If so, why is the next statement ford.new Gear(1,12.3) ?

An instance of an inner class must be created in the context of an instance of its enclosing outer class. The ford.new Gear(...) is saying create the new Gear instance in the context of the Gearbox instance that ford refers to.

(If that still doesn't make sense, reread the above paying special attention to usage of the the word "instance". An instance of a class is an object .....)


As @Thilo points out, making Gear an inner class (rather than a nested class) here does not really achieve anything. It would probably be better to a redeclare Gear as

    public static class Gear ...

and then you would not need to qualify the new with an instance of the Gearbox class.

Upvotes: 2

Related Questions