Wednesday, 31 May 2017

Tutorial 32: Inner Classes



So far, we have looked at anonymous classes. Anonymous classes are an example of inner classes. What inner class means is that it is a class that is defined within the body of another class. In this tutorial, we will look at three other instances where inner classes can be used.

Non-static Inner Classes

Let’s create a class called Person and create a non-static inner class called Bird within its body:

public class Person {
            private String name;
            private int id;
            class Bird {

            }
}

Note that the inner class Bird will have access to the instance data of the Person class, i.e. private String name and private int id. So, with non-static inner classes, it is possible to do this:

public class Person {
            private String name;
            private int id;
           
            public Person(String name, int id) {
                        this.name = name;
                        this.id = id;
            }

            class Heart {
                        public void beat() {
                                    System.out.println("The heart of " + name +  " of id " + id + " pumps");
                        }
            }

            public static void main(String[] args) {
                        Person person1 = new Person("Brian", 5);
                        Person.Heart heart1 = person1.new Heart();
                        heart1.beat();
            }
}


Figure 1: Non-static inner classes


The purpose of inner classes is just for grouping purposes. For example, If you are defining a class to control the cockpit screen of an aircraft, you might define within a class CockpitScreen, inner classes such as Altimeter, AttitudeIndicator, FuelGaugeIndicator and so on.

It is important to note that when you create an instance of the outer class, you don’t automatically create an instance of the inner classes. This means that you have to explicitly do it somewhere. 


Static Inner Classes


public class Person {
            private String name;
            private int id;
            static class Eye {
                        public void blink() {
                                    System.out.println(“The eye is blinking”);
            }
}
}

Because the class Eye is static, we cannot access non-static instance variables such as private String name and private int id. However, you can access static variables from the static inner class. So, if either private String name or private int id was static, we could refer to it within the class Eye and everything would be okay.

So, what is the purpose of a static inner class? Just as before, inner classes are created for the purpose of grouping. So we can write and run the following code no problem.

public class Person {
            private String name;
            private int id;
            static class Eye {
                        public void blink() {
                                    System.out.println(“The eye is blinking”);
            }
            public static void main (String[] args) {
                        Person.Eye eye = new Person.Eye();
                        eye.blink();
}
}



Figure 2: Static inner classes


So let’s look again at the different scenarios in which you’d use a static and non-static inner class. You create a non-static inner class when you want the inner class to be able to use the instance variables of the enclosing outer class. However, you use static inner classes when you want to group the class as part of the outer enclosing class, but without associating it with instances of the enclosing class. So, other than the fact that static inner classes are grouped together with the enclosing outer class, it is just a normal class.

Classes Declared Within Methods


Java allows us to declare classes within methods. If the class you declare within the method is non-static, it has access to the instance variables of the enclosing outer class.

public class Person {
            private String name;
            private int id;
            public void walk() {
                        System.out.println(“Walking”);
                        class Limp {
                                    public void limp() {
                                                System.out.println (“Person ” + name + “ id ” + id +
                                                            “ is limping.”
                                    }
}
}

A class declared inside a method also has access to the objects declared within the method. The only caveat is that those variables within the method have to be declared final. However, it can access instance variables of the overall enclosing class just fine.

public class Person {
            private String name;
            private int id;
            public void walk() {
                        System.out.println(“Walking”);
                        final int length = 55;
                        class Limp {
                                    public void limp() {
                                                System.out.println (“Person ” + name + “ id ” + id +
                                                            “ is limping.”);
                                                System.out.println (“Length is: ” + length);
                                    }
}
}



Figure 3: Classes declared within a method


Remember that classes declared within a method can only be instantiated and used within the same method, because the method within which they are declared determines its scope. Moreover, you cannot use access specifiers such as public, private or protected on inner classes declared within methods because the scope of the class is within the method in which it is declared.

That’s all for inner classes.

If you have any questions or comments, please drop them in the comments section below and I will answer them as soon as possible.

Until next time, stay safe.  

No comments:

Post a Comment