Core JAVA
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Java Inner Classes
Class within a class is called inner class. Java Inner classes are useful if we need some classes within the same class to do some processing and not required outside class.
The class which is written inside a class is called a nested class and a class that holds a nested class is called outer class.
We can divide nested class into two categories:
- Non Static Nested classes.
- Static Nested Classes.
Inner Classes / Non Static Nested classes
Non-static classes within a class are non-static nested inner classes.
Based on implementation in code we can categorize them into 3:
- Normal Inner Class.
- Inner Class In A Method.
- Anonymous Inner Class
Normal Inner Class
Class within a class without any static keyword is normal inner class.
In the below example MyOuterClass is outer class and MyNormalInnerClass is inner class and InnerClassExample’s main method is showing how to use them.
Example:
public class InnerClassExample { public static void main(String[] args) { MyOuterClass myNormalInnerClass = new MyOuterClass(); myNormalInnerClass.accesInnerClass(); } } //Outer class class MyOuterClass { public void accesInnerClass() { MyNormalInnerClass obj = new MyNormalInnerClass(); obj.printFromInnerClass(); } //Inner class class MyNormalInnerClass { void printFromInnerClass() { System.out.println("Hello from normal inner class."); } } }
Output:
Hello from normal inner class.
Inner Class In A Method
Inner class defined within a class’s method is called inner class in a method.
In the below example MyOuterClass is outer class and MyNormalInnerClass is inner class within method myMethod() of outer class.
Example:
public class InnerClassExample { public static void main(String[] args) { MyOuterClass myOuterClass = new MyOuterClass(); myOuterClass.myMethod(); } } class MyOuterClass { //Method public void myMethod() { //Inner class class MyNormalInnerClass { void printFromInnerClass() { System.out.println("Hello from method inner class."); } } MyNormalInnerClass myNormalInnerClass = new MyNormalInnerClass(); myNormalInnerClass.printFromInnerClass(); } }
Output:
Hello from method inner class.
Anonymous Inner Class
A class which is defined along with creation and no other instance of class can be created is called anonymous class and such class within a class is called anonymous inner class.
Syntax:
public class MyOuterClass{
MyAnonymousInnerClass obj = new MyAnonymousInnerClass{
//Code
}
}
In the below example InnerClassExample is acting as an outer class and MyAnonymousInnerClass is an anonymous inner class.
Example:
public class InnerClassExample { //This can be interface, class or abstract interface MyAnonymousInnerClass { void myMethodToPrint(); }; public static void main(String[] args) { //Anonymous inner class MyAnonymousInnerClass innerClass = new MyAnonymousInnerClass() { @Override public void myMethodToPrint() { System.out.println("Hello from anonymous class method"); } }; innerClass.myMethodToPrint(); } }
Output:
Hello from anonymous class method
Static Nested Inner Class
Static inner class is used similar to normal inner class where a class is defined within another class but with static keyword and has static nature.
Syntax
class MyOuterClass {</p><p>static class MyInnerStaticClass {</p><p>}<br />}
In below example InnerClassExample class is acting as outer class and MyStaticInnerClass is acting as a static inner class.
Example:
public class InnerClassExample { //Inner class static class MyStaticInnerClass { static void printFromInnerClass() { System.out.println("Hello from static inner class."); } } public static void main(String[] args) { MyStaticInnerClass myNormalInnerClass = new MyStaticInnerClass(); myNormalInnerClass.printFromInnerClass(); } }
Output:
Hello from static inner class.
References
Next Article To Read
If you have not read our previous article on Java Exceptions may read as well. You my read our next article on OOPs concetps: