Sunteți pe pagina 1din 10

JAVA, STRUTS 2, SPRING, HIBERNATE, SOLR,

MAHOUT AND HADOOP TUTORIALS TIPS AND


TRICKS
Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr,
XML, JSON Examples, Hibernate & Struts 2 Hello World projects

HOME

JAVA

SPRING

HIBERNATE

JSP & SERVLET

JDBC

SOLR

OTHERS
Go to...

Java Inner Classes - Example and


Explanation
NAGESH CHAUHAN 07:43 CORE-JAVA NO COMMENTS

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.

Please note that we are not covering Anonymous inner classes


and Static nested classes here. We are just talking about regular
inner classes and their properties. Anonymous inner classes and
Static nested classes are special case of inner classes that are
covered in other tutorials.

How to create an inner class in Java


An normal inner class can be created in any class, with any type of access modifier
(public, private, protected & default).
view plainprint?

1. [modifier] class OuterClass {


2.
3.

[modifier] class innerClass {

4.

5. }

Java Inner Class Properties


1) We can create a long hierarchy of inner classes as long as we want to :
view plainprint?

1. private class OuterClass {


2.

public class InnerClassA {

3.

public class InnerClassB {

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.

public void printMe() {

7.

System.out.println("I am inner class !");

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.

static void callInner() {


/*

10.

* way of creating an inner class object in static method of outer class

11.

*/

12. OuterClass ouetClass = new OuterClass();


13. OuterClass.InnerClass inner = ouetClass.new InnerClass();
14.
15. /*
16.

* another way of creating an inner class object in static method of

17.

* outer class

18.

*/

19. InnerClass inner1 = new OuterClass().new InnerClass();


20. }
21. }

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.

* way of creating an inner class object in static method of outer class

14.

*/

15. OuterClass ouetClass = new OuterClass();


16. OuterClass.InnerClass inner = ouetClass.new InnerClass();
17.
18. /*
19.

* another way of creating an inner class object in static method of

20.

* outer class

21.

*/

22. OuterClass.InnerClass inner1 = new OuterClass().new InnerClass();


23. }
24. }

5) No inner class object is automatically instantiated with creation of outer classs


object.
6) An inner class have free access to all members of its outer class, no matter what
the access level of outer class members has.
view plainprint?

1. package com.beingjavaguys.com;
2.
3. public class OuterClass {
4.
5.

String def = "default";

6.

public String pub = "public";

7.

private String pri = "private";

8.

protected String pro = "protected";

9.
10. class InnerClass {

11. void printMe() {


12.

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.

public String pub = "Outer - public";

6.
7.
8.

class InnerClass {
public String pub = "Inner - public";

9.
10. void printMe() {
11.
12.

// i am calling local vaiable

13.

System.out.println(pub);

14.
15.

// i am calling outer class variable

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.

String outerVar = "outerVar";

6.
7.
8.

void callInner() {
/*

9.

* inner class members can be called in a outer class using inner

10.

* class's instance, no matter what the access specifier is

11.

*/

12. InnerClass innerClass = new InnerClass();


13. System.out.println(innerClass.def + "" + innerClass.pub + ""
14.

+ innerClass.pri + "" + innerClass.pro);

15. innerClass.callOuter();
16.
17. }
18.
19. class InnerClass {
20. String def = "default";

21. public String pub = "public";


22. private String pri = "private";
23. protected String pro = "protected";
24.
25. void callOuter() {
26.

/*

27.

* outer class members can be called directly in a inner class, no

28.

* matter what the access specifier is

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.

Compilation and class file generation for inner classes.


As we have discussed an outer class can have a number of inner classes in it, each
inner class will have a separate class file generated by compiler. Consider following
example.
view plainprint?

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.

S-ar putea să vă placă și