💻Step 11:Nested Classes – 7th hour + code

Learn with youtube video- 

 

💡Definition

-class inside another class is called as nested class.

Quick comparison between different types of nested classes in Java:

Type of Nested ClassDefinitionCan be Declared Static?Can be Declared Final?Can Access Outer Class Members?
Static Nested ClassA static nested class is a class that is defined within another class and is marked with the static keyword. It is a full-fledged member of the outer class and can be accessed using the outer class name.YesYesYes
Instance Inner ClassA non-static nested class is a class that is defined within another class and is not marked with the static keyword. It is also known as an inner class. Inner classes have an implicit reference to the outer class and can access all the members (including private members) of the outer class.NoNoYes
Local Inner ClassA local inner class is a class that is defined within a block of code, such as a method or a for loop. It can only be accessed within the block of code in which it is defined and does not have an access modifier.NoNoYes
Anonymous Inner ClassAn anonymous inner class is a class that is defined and instantiated in a single line of code and does not have a name. It is usually used as a shortcut when you need to define a class that is used only once and does not need to be reused.NoNoYes

💡1- static nested class

It’s is static class which is defined inside another class.

💡static nested class can’t access instance variable directly

sttclass

💡2- non static class(Inner classes)

💡2.1- member class

It’s is class which is defined inside another class.

mmbrclas

💡member class can access instance variable directly.

mmbrclass

💡2.2- Local class

💡It’s is class which is defined and used inside the method another class.

localmmbrclass1

💡2.3- Anonymous class

💡It’s is nameless class which is defined inside body of constructor call.

anoymous

code samples are here
1-Program to create static nested class ‘SmallStatic’ inside Outer class StaticInner and then create object of static nested class ‘SmallStatic’.

package innerClasses;

public class StaticInner { 
static class SmallStatic{ 
SmallStatic(){
System.out.println("i am from static class");
}
} 
public static void main(String[] args) { 
StaticInner.SmallStatic static1=new StaticInner.SmallStatic(); 
} 
}

Run this Program ▶️

2- Program to create member class ‘SmallMember’ inside Outer class MemberOut and then create objec of member class ‘SmallMember’.
package innerClasses;
public class MemberOut { 
class SmallMember{ 
SmallMember(){
System.out.println("i am called");
}
} 
public static void main(String[] args) { 
MemberOut.SmallMember member=new MemberOut().new SmallMember(); 
} 
}
 
 
3-  Program to create Local class ‘SmMth’and create its object inside method ‘shw2’ of Outer class MethodLocOut.
package innerClasses;

public class MethodLocOut { 
void shw2(){ 
class SmMth{ 
SmMth(){
System.out.println("i am called"); 
}
} 
SmMth mth=new SmMth();
} 
public static void main(String[] args) { 
MethodLocOut mLI=new MethodLocOut(); 
mLI.shw2(); 
}
}
 
 4-  Program to create Anonymous class with constructor of Outer class AnonCls.
package innerClasses;

public class AnonCls {
void show(){
System.out.println("hiui");
}

public static void main(String[] args) {
AnonymousClss clss=new AnonymousClss(){
void show(){
System.out.println("hiii");
}
};
clss.show();
}
}
 

Interview Questions —

  • Are nested and inner classes same?
  • What is difference between non-static and static class?
  • What is difference between instance, local and anonymous class?

Leave a Reply

Your email address will not be published. Required fields are marked *