Wednesday 28 August 2013

Inner classes.

when a class declared inside the class is known as nested class or inner class.
we can connect class logically in a group to provide more readability and maintainability which also provide encapsulation.
it can also access all the members of the class including private.

Example:-basic format

Types of inner classes;-
  1. instance inner class
  2. static inner class
  3. local inner class
  4. anonymous inner class

Instance inner class;-in non static inner class we cannot develop any static member.

Example:-


in the above example we use static member inside non static inner class so it give compile time error.

Static inner class:-in static inner class we can develop both static and non static member but we cannot use non static member .

Example:-


in this we use non static member inside static inner class.so it will give compile time error.

Local inner class:-A class present inside the definition block or in the method is known as local inner class.
we cannot develop static member inside local inner class.

Example:-

inside local class if we use any variable or member of outer class then the member should be final.

Anonymous inner class:-the class which has no name and pointed by a reference variable is known as anonymous inner class and it is used for one time only.
we cannot develop constructor inside anonymous class.
Example:-


the highlighted part is the anonymous inner class in which we implement the test1() method.

NOTE:-Any class can be used anywhere if the usage of the class is as a datatype.

Example:-


in this we use class A and B as a datatype.and A is non static and B is static no error occurs.
for using as a datatype we write Outer.Inner.







No comments :

Post a Comment