HOME
JAVA
SPRING
HIBERNATE
JDBC
SOLR
OTHERS
Go to...
Inner Class in java so called nested class is nothing but a class that is being defined
inside another class. Inner classes are beneficial in two scenarios i.e. access control
and naming control. Although these benefits can be acquired using java packages.
4.
5. }
3.
4.
5.
6.
}
}
2) Outer class can create as many numbers of instances of inner class inside its
code.
view plainprint?
1. package com.beingjavaguys.com;
2.
3. public class OuterClass {
4.
5.
class InnerClass {
6.
7.
8.
9.
}
}
10.
11. void callInner(){
12. InnerClass inner = new InnerClass();
13. inner.printMe();
14.
15. InnerClass inner1 = new InnerClass();
16. inner1.printMe();
17. }
18. }
3) The instance of a inner class in static method of outer class can be created in
either of the two ways.
view plainprint?
1. package com.beingjavaguys.com;
2.
3. public class OuterClass {
4.
5.
class InnerClass {
6.
7.
8.
9.
10.
11.
*/
17.
* outer class
18.
*/
4) To create an instance of inner class in another class, other than the outer class
we must have a instance of outer class.This can be done in either of the two ways.
view plainprint?
1. package com.beingjavaguys.com;
2.
3. public class OuterClass {
4.
5.
class InnerClass {
6.
7.
8. }
9.
10. class AnotherClass {
11. static void callInner() {
12. /*
13.
14.
*/
20.
* outer class
21.
*/
1. package com.beingjavaguys.com;
2.
3. public class OuterClass {
4.
5.
6.
7.
8.
9.
10. class InnerClass {
System.out.println(def + " " + pub + " " + pri + " " + pro);
13. }
14. }
15.
16. }
7) In case the inner class have same variable name as the outer class, than outer
class variable can be called as follows:
view plainprint?
1. package com.beingjavaguys.com;
2.
3. public class OuterClass {
4.
5.
6.
7.
8.
class InnerClass {
public String pub = "Inner - public";
9.
10. void printMe() {
11.
12.
13.
System.out.println(pub);
14.
15.
16.
System.out.println(OuterClass.this.pub);
17. }
18. }
19.
20. }
8) Inner class members can be accessed by outer class with the instance of inner
class, no matter what the access specifier they have. Outer class members can be
accessed within inner classes directly, no matter what the access specifier they
have.
view plainprint?
1. package com.beingjavaguys.com;
2.
3. public class OuterClass {
4.
5.
6.
7.
8.
void callInner() {
/*
9.
10.
11.
*/
15. innerClass.callOuter();
16.
17. }
18.
19. class InnerClass {
20. String def = "default";
/*
27.
28.
29.
*/
30.
callInner();
31.
System.out.println(outerVar);
32. }
33.
34. }
35.
36. }
9) We can not have a static member in an inner class, not even public static void
main(). If we does so, the inner class should be declared static, and this is not the
case of inner class, static inner classes are special case and are called static nested
classes.
1. package com.beingjavaguys.com;
2.
3. // Outer class
4. public class OuterClass {
5.
6.
// Inner class2
7.
class Inner1 {
8.
9.
10. // Inner class2
11. class Inner2 {
12. }
13.
14. public static void main(String[] args) {
15.
16. // Anonymous inner class1
17. new Object() {
18. };
19.
20. // Anonymous inner class2
21. new Object() {
22. };
23.
24. }
25.
26. }
In the code above we have an outer class, two normal inner classes and two
anonymous inner classes. On compilation there will be five class files generated.
1) Outer.class
2) Outer $Inner1.class
3) Outer $Inner2.class
4) Outer$1.class
5) Outer$2.class
Here we are done with normal inner classes in java and their properties, in our
upcoming blogs we will see Anonymous inner class & Static nested classes and
more about java.