Advertisement
Home arrow Articles arrow JDBC arrow Brief tutorial on Java Inner classes
Brief tutorial on Java Inner classes E-mail
Written by marc castrovin   
Article Index
Brief tutorial on Java Inner classes
Part - 2

This is a brief article on Java Inner Classes.

I will focus on the following:

  • What are Inner Classes?
  • Categories of Inner Classes
  • Using Inner Classes


related links



Inner classes are classes that are defined within the block of another class

1 public class EnclosingClass
2 {
3 public class MyInnerClass
4 {
5 . . .
6 }
7 . . .
8 }


Image

These category names are widely used and descriptive, but are not actually defined in the Java documentation.

Why Use Inner Classes?

  • If a given class is used only by a single class, you can simplify the design by using an inner class
  • Inner classes let you explicitly show that a class depends on another
  • Anonymous local inner classes are very handy to hook up listeners to JavaBean events
  • Downside: Inner class syntax is a bit obscure

Static Member Inner Classes

Static member inner classes are defined as static within the enclosing class

1 public class MyEnclosingClass
2 {
3 . . .
4 static public class InnerClass3
5 {
6 private int y = 14;
7
8 public void myMethod()
9 {
10 System.out.println(y);
11 }
12 }
13 . . .
14 }



Here we show defining a static member inner class that itself defines a private field and a public method. Within the inner class, you are free to use any of the class definition techniques available to a non-inner class.
Since the inner class is defined as public, it is accessible from outside of the enclosing class. If we had defined it as private, then it would only be visible within the enclosing class.


Accessing Enclosing Class Members


Static member inner classes can access any non-static field or method defined in the enclosing class

1 public class MyEnclosingClass
2 {
3 private int x = 12;
4 static private int y = 20;
5 static private int z = 21;
6
7 static public class InnerClass3
8 {
9 private int y = 14;
10
11 public void myMethod()
12 {
13 System.out.println(y);
14 System.out.println(z);
15 System.out.println(MyEnclosingClass.y);
16 }
17 }
18 . . .
19 }

The inner class can access private members of the enclosing class. That makes sense if you consider that the inner class is itself considered a member of the enclosing class.
Note the odd syntax that's required if the inner class defines a field with the same name as a field in the enclosing class.


Instantiating Static Member Inner Class Objects

  • From within a static method in the enclosing class, you can use normal new operator syntax
  • From outside the enclosing class, you use new and specify both the enclosing and static member inner class name

Inside enclosing class

InnerClass3 m3 = new InnerClass3();

Outside of the enclosing class
MyEnclosingClass.InnerClass3 m3 =
new MyEnclosingClass.InnerClass3();

The syntax to instantiate a static member inner class object from outside of the class is a bit weird, but is consistent with the syntax for accessing any kind of static member.
Note that in either case, since the inner class is static, you don't have to first create an instance of the enclosing class. We will see that with non-static member classes, you will need to first create an enclosing class object.


Non-Static Member Inner Classes

- Non-static member inner classes are defined within the boundary of the enclosing class

1 public class MyEnclosingClass
2 {
3 . . .
4 public class InnerClass2
5 {
6 private int x = 14;
7
8 public void myMethod()
9 {
10 System.out.println(x);
11 }
12 }
13 . . .
14 }

Non-static member inner classes are syntactically similar to static -- just omit the static keyword. You instantiate objects and access them a bit differently, however.


Accessing Enclosing Class Members

- Non-static member inner classes can access any of the enclosing class fields or methods

1 public class MyEnclosingClass
2 {
3 private int x = 12;
4
5 static public class InnerClass3
6 {
7 private int x = 14;
8 private static int y = 17;
9
10 public void myMethod()
11 {
12 System.out.println(x);
13 System.out.println(y);
14 System.out.println(MyEnclosingClass.this.x);
15 }
16 }
17 . . .
18 }

Non-static member inner classes can access any of the enclosing class's members, be they private or
not, static or not.
Note the odd syntax required if an inner class defines a field with the same name as field from the enclosing class. The implication is that within an inner class, there are actually two "this" references -- the "this" for the inner class itself and the "this" for the enclosing class.




 

Sponsored

Login / Logout


Search HotJobs now for jobs
© 2007 Anil Kumar Kuchana SkillFox.com