0% found this document useful (0 votes)
49 views6 pages

Nested Classenotess

The document discusses different types of nested classes in Java including static nested classes, inner classes, method local inner classes, and anonymous inner classes. It provides examples of each type and notes on their characteristics such as accessibility to outer class members and ability to declare static members.

Uploaded by

300allinone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views6 pages

Nested Classenotess

The document discusses different types of nested classes in Java including static nested classes, inner classes, method local inner classes, and anonymous inner classes. It provides examples of each type and notes on their characteristics such as accessibility to outer class members and ability to declare static members.

Uploaded by

300allinone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

>Nested classes - 1

class Outer
{
class Inner
{}
}

nested classes : java 1.1

1. static nested classes


2. non-static nested classes (inner classes)
a. normal inner classes
b. method Local inner classes
c. anonymous inner classes

ex:

class Outer
{
int a=10, b=201;
void ml()
{
m2(); //not possible
}
class Inner
{ int x=100,y=200;
void m2()
{
m1();
}
}
}

Note:-

1. Outer.class
Outer$Inner.class

2. Outer o = new Outer();


Outer.Inner i = o.new Inner();
3. o.a o.b o.m1()
i.x i.y i.m2()
4. Inner cls methods can access outer cls data members.
Outer cls methods unable to access inner cls data members.
Introduction
One Functionality can exist with the presence of another functionality then we go to inner class

nested classes - 2 Advantages

1.
class A
{ class B
{}
}

a. all the Logics in single place : encapsulation : grouping data


b. Readability increase
c. inner CLs can access outer CLs private properties

2.
class Testa
{
Void m1(){}
void m2(){cLass X{}}
Void m10(){}
}

3. one time usage : Logics required only one time


4.one functionality can exist with the presence of another functionality we require
inner class concept

Ex:-

class BÄnk
{ class Account
{
}
}

Ex:-

class Cotg
{ class Dept
{
}
}
Note:- (1)The Account Functionally can exit with the presence of Bank.
Because to create the object of Account class , Bank class object in mandatory.
(2) To create the object of inner class, outer class object in mandatory.

Nested classes-3 (Normal inner classes)

Nested classes-4 (Method local inner classes)

Note:-
A class which is declare inside a method is called method local inner class

 In application level there are so many method. It’s possible each method contain inner
classes
 Inside a method we can declear multiple classes
 Its possible one class extend another class
 The scope of the class is only within the method
 Outer class five modifires are applicable : public, default, final, abstract, strictfp
 Inner class eight modifires are applicable : public, default, final, abstract, strictfp ,
static, private, protected
 Inner class abstract (cannot be instantiated)& final (inheritance not possible) modifier
are possible

Ex:- 1

public class Outer {


void m1() {
class inner{
void dis() {
System.out.println("Method local inner
class");
}
}
new inner().dis();
}
public static void main(String[] args) {
new Outer().m1();
}
}
Ex:-2 we can declare inner class inside the for loop also

public class Outer {


void m1() {
for (int i = 0; i < 5; i++) {
class inner {
void dis() {
System.out.println("Method local
inner class");
}
}
new inner().dis();
}
}

public static void main(String[] args) {


new Outer().m1();
}
}

Nested classes-5 (Static nested inner classes)

Note:-

 The normal inner class can access both static and instant
member of outer class.
 The static inner class can access only static member of outer
class.
 In normal inner class static declarations are not possible
 In static inner class static declarations are possible
 In normal inner class main method, static blocks are not
possible
 In static inner class main method, static blocks are possible
 Outer.inner I = new Outer(). New Inner(); //// normal Inner object
creation
 Outer.Inner I = new Outer().Inner(); // static Inner object
creation

Ex - 1
public class StatiNestedClasses {
static int i=10;
static int j=20;
static class Inner{
int a=30;
static int b=40;
void dis() {
System.out.println(a+b);
System.out.println(i+j);
}
}
public static void main(String[] args) {
//new StatiNestedClasses().new
Inner().dis();
new StatiNestedClasses.Inner().dis();
}
}

Nested classes-6 (Anonymous inner classes)

Note :-

 The nameless inner classes is called Anonymous inner classes.

Ex 1:

Application without anonymous


class A
{ void disp() {}
}

class Test extends A


{ void disp() {System. out. println( "Good Morning"); }
}

class TestCLient
{ public static void main (String[] args)
{ Test t = new Test();
t.disp();
}
}

//Application with anonymous


class A
{ void disp() {}
}

class TestClient
{ A a = new A ()
{ void disp() { System.out.println("Good Evening");
System.out.println(a.getClass().getName()); }
};
public static void main(String[] args) {
TestClient t = new TestClient();
t.a.disp();
}
}

Output :-
Good Evening
TestClient$l

You might also like