0% found this document useful (0 votes)
26 views7 pages

19 Day Method Local Inner Class

The document discusses inner classes in Java, categorizing them into static and non-static types, with a focus on method local inner classes and anonymous inner classes. It explains the rules and limitations of local inner classes, including their inability to use access specifiers and the requirement for local variables to be final (or effectively final in JDK 8). Additionally, it contrasts aggregation and composition, highlighting their differences in dependency and relationship types.
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)
26 views7 pages

19 Day Method Local Inner Class

The document discusses inner classes in Java, categorizing them into static and non-static types, with a focus on method local inner classes and anonymous inner classes. It explains the rules and limitations of local inner classes, including their inability to use access specifiers and the requirement for local variables to be final (or effectively final in JDK 8). Additionally, it contrasts aggregation and composition, highlighting their differences in dependency and relationship types.
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

Inner class is Categorized in 2 category

1) Static Inner class


2) Non Static Inner class
2.1) Regular Inner class
2.2) Method Local Inner class
2.3) Anonymous Inner class
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2.2) Local inner class / Method Local Inner class:


A class that is created inside a method/constructor/block is known as Local Inner class.

If you want to call the methods of Local inner class, you must create the object of this class inside

the method only because it is local to method.

Ex:

[[Link]]
//This demo is for method local inner class
class Outer
{
// instance variable
int x=10;

// class variable/static variable


static int y=20;

void display()
{
//Method Local inner class
class Inner{
public void show(){
[Link](x);
[Link](y);
}
} // Method Local inner class End

Inner i=new Inner();


[Link]();
} //Method End

public static void main(String args[])


{
Outer o= new Outer();
[Link]();
}
}

Note:
We can not use the Local variable in the Local inner class but we can use the outer class variable.

If u want to use the local variable in the Method Local Inner class in that case that local variable

Must be final.

But From the JDK 8 no need to use the final keyword before the local variable bcz it

is Automatically used.

Ex:

class Outer
{
//instance variable
private int x=10;

//instance method
void display()
{
//local variable
int value=50;

//Method Local Inner class


class Local
{
void show(){
[Link](x); //Ok
[Link](value); //CTE / But From jdk8 Now it is also ok
}
} //Method Local class End

Local l=new Local();


[Link]();
} //Method End
public static void main(String args[]){
Outer o=new Outer();
[Link]();
} //main end
} //class end

Why we use the method Local inner class


When we want to create more than one class with the same name within the outer class in that case
Method Local inner class is used.

Ex:

class Tester
{
void method1(){
class LocalInner{ ….. }
}
void method2(){
class LocalInner{ ……. }
}
}

Disadvantage of Local Inner Class:


Rule:
Method Variable / Local variable can't be private, public or protected.
That is why we can not use any access specifier before the Method Local
Inner class.
Ex:

void display()

public int var=10; // CTE

public class Local //Not okk / CTE

}
So We can not use any access specifier before the Method Local Inner class. Method Local Inner class
can not be static as well.

Rules for Local Inner class


Local inner class can not be invoked/called from outside the method it means every thing has to be

done within the method only.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2.3) Annonymous Inner class:


A class that has no name is known as anonymous inner class.

It means defining a class with no identity.

It is categorized in two categories.

1) Flavor One an Anonymous Inner class extends another class.

2) Flavor Two an Anonymous Inner class implements Interface.

Ex:

[[Link]]
// Flavor One an Anonymous Inner class extends another class.
class Parent
{
public void display()
{
[Link]("Hello I am base class Func");
}
} //class End
class Tester
{
Parent p = new Parent() {
//overriding
public void display() {
//[Link](); //It invoke the parent class disp()
[Link]("Hello i am child class func");
}
};
public static void main(String args[])
{
Tester obj=new Tester();
[Link]();
}
}

Ex:

[[Link]]

// Flavor Two an Anonymous Inner class which implements Interface.


interface Cookable
{
public abstract void cook();
} // Interface end

class Food
{
Cookable c = new Cookable() {
public void cook() //Overriding
{
[Link]("anonymous cookable implementer");
}
};
public static void main(String[] args)
{
Food obj=new Food();
[Link]();
}
}

> javap Food$1

Ex:

//Program of anonymous inner class by abstract class


abstract class Person
{
public abstract void eat();
} //class end
class Emp
{
public static void main(String args[])
{
Person p=new Person() {
public void eat()
{
[Link]("nice fruits");
}
}; // anonymous inner class end
[Link]();
} //main end
} // class end

Important facts of Anonymous class:


-- > Anonymous class does not have any name that is why we can not have the constructor in

The anonymous inner class.

--> Anonymous class can be defined and instantiated simultaneously that is why it cannot be

Instantiated more than once it is basically used in reference passing.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Aggregation vs Composition
1. Dependency:
Aggregation implies a relationship where the child can exist independently of the
parent.
For example, Bank and Employee, delete the Bank and the Employee still exist.
Whereas Composition implies a relationship where the child cannot exist
independent of the parent.
Ex:
Human and heart, heart don’t exist separate to a Human.

2) Type of Relationship:
Aggregation relation is “has-a” and composition is
“part-of” relation.

3) Type of association:
Composition is a strong Association whereas Aggregation is
a weak Association.

You might also like