Continuing with our discussion of inner classes, this post will deal with how to create inner classes from outside the outer class instance code and also deal with the next type of inner class (inner classes declared inside methods)
Recalling a few crucial points from last post, it should be remembered that
- An instance of inner class always exists only if an instance of outer class exists. Inner classes never exist alone.
- Inner classes always have access to all the methods and members of the outer class even if they are declared private.
In the previous post, we learnt how to create an instance of inner classes from the outer class with an example. The code below will create an instance of inner class from outside the outer class instance code (and inside the static method):
package whizlabs; //Outer class public class outer { private int i=6; // Inner class class inner3{ void simple(){ System.out.println(i); } }
public static void main(String[] args) { //Create an instance of outer class outer o=new outer(); //Create an instance of inner class outer.inner3 e=o.new inner3(); e.simple(); } }
Creating an ‘instance of inner class from outside the outer class instance code’ is similar to creating an ‘instance of inner class from the outer class’ (which almost sounds like a tongue twister!) ,but since there is no “this” reference, an instance of outer class is first created and the inner class is then created as follows: (Kathy Sierra)
//Create an instance of outer class outer o=new outer(); //Create an instance of inner class outer.inner3 e=o.new inner3();
In the above example, ‘outer’ is the outer class which has only one private integer variable ‘i’ and ‘inner3’ is the inner class which has only one method which prints out the variable ‘i’. As expected, this is the value that is produced on output:
6
Method-local inner classes:
Having seen “plain” inner classes and the two ways to create instances of it, we next move onto method-local inner class. So far, we have only worked with inner classes being created inside an outer class. What if the inner class is created inside a method? How will we work with such classes?
package whizlabs; public class method_local { private int j=5; void add(){ // creation of inner class inside a method class inner{ void inner_method(){ System.out.println(j); } } // Method-local inner class is instantiated inner i=new inner(); // Must be instantiated here i.inner_method(); } public static void main(String[] args) { // TODO Auto-generated method stub method_local m= new method_local(); m.add(); } }
Outer class: method_local
Outer class method: add()
Outer class private variable: j
Method-local inner class: inner
Method –local inner class method: inner_method()
The code above just prints out the value of the private integer variable ‘j’. But the important point to note is the creation of the inner class, within the outer class method and instantiating it in the correct place. Here are the key takeaways for the method-local inner classes:
Point 1: Method-local inner classes must be instantiated in the method where they are declared.
Method-local inner classes are like other inner classes in that they can access all the members of the outer class (including private ones) except for one point which is explained below.
Point 2: Method-local inner classes cannot access the local variables of the method where they are declared except when the local variables are declared ‘final’.
This is because local variables of a method will not live as long as the inner class itself.
Having seen method-inner classes and other quirkiness of inner classes, we will move onto anonymous inner classes in the next post.
Bibliography
SCJP 6. In B. B. Kathy Sierra.
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021