Inner Classes (Non-Static Nested Classes)

Inner classes are the second category of nested classes. Again, there are two key things you must know about them:

They have access to the members of the enclosing class (even private)

Since they are associated with an instance (an object), they can’t define any static members themselves (except constant variables, i.e. primitive type or String, declared final and initialized with a compile-time constant expression)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

publicclassEnclosingClass{

privateinti;

classInnerClass{

staticintj;// compiler error: declaring static member that is not a constant

staticfinalintk=5;

voidfoo(){

// An inner class have access to the enclosing class members

i++;

}

}

publicvoidouterFoo(){

// creation from the enclosing class

InnerClass ic=newInnerClass();

}

}

classOther{

publicvoidotherFoo(){

// creation from outside the enclosing class

EnclosingClass.InnerClass ic=newEnclosingClass().new InnerClass();

}

}

There are two specialized types of inner classes:

Local classes

Anonymous classes

Local Classes

They are class declarations inside a block and:

They have access to local variables declared final

Inside static methods they can only refer to static members of the enclosing class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

classEnclosingClass{

inta=2;

staticintb=3;

// non-static method of the enclosing class

voidfoo(){

finalinti=1;

intj=2;

classLocalClass{

voidlocalFoo(){

// final variable "i" is accessible

System.out.println(i);

// compiler error: non-final "j" is not accessible

j++;

// members of enclosing class are accessible

a++;

b++;

}

}

}

// static method of the enclosing class

staticvoidstaticFoo(){

classLocalClass{

voidlocalFoo(){

// non-static member of enclosing class is not accessible

a++;

// static member of enclosing class is accessible

b++;

}

}

}

}

Anonymous Classes

They are expressions of two kinds, either implementing an interface (new InterfaceName(){ … }) or extending another class (new ClassName(args) { … }). In both cases:

They have access to local variables declared final

They can’t declare constructors

The following is an example of an anonymous class extending the EventHandler class:

1

2

3

4

5

6

7

EventHandlerh=newEventHandler<ActionEvent>(){

publicvoidhandle(ActionEvent event){

System.out.println("Anonymous!");

}

});

That’s all coders! If you have any comments or questions, you can drop me a line or leave a comment below and I will be pleased to help you.

In case you’re looking for a complete (but still short, precise and to-the-point) study guide to help you get your valuable Oracle® Java Programmer certification, including many tricky real-exam-like questions, study sheets, and other bonuses, then the Java Certification Roadmap book is your book.

Finally, if you found this content useful and know more programmers, colleagues or friends that may be interested in getting the Java Certification, click one of the buttons belowclick the button on the bottom left corner of your screen to let them know about the Java Certification Roadmap.