Home core java Java nested class overview with examples

Java nested class overview with examples

1. What is java nested class

The Java programming language allows you to define a class within another class. Such a class is called nested class. A java nested class is a class within a class.

The relationship between outer class and inner class is not IS-A relationship and it is Has-A relationship. Interfaces also can be nested. Following are possible inner classes.

1.1. Example 1 a class with in class

//outer class
class OuterClass {
      //inner class
      class InnerClass {
      }
}

1.2. Example 2 an interface with in interface

- Advertisement -
//outer interface
interface OuterInterface {
      //inner interface
       interface InnerInterface {
       }
}

1.3. Example 3 an interface within a class

//outer class
class OuterClass {
     //inner interface
     interface InnerInterface {
     }
}

1.4. Example 4 a class with in an interface

//outer interface
interface OuterInteerface {
     //inner class
     class InnerClass {
     }
}

1.5. Example 5 a static class or interface with in a class

//outer class
public class OuterClass {
	
	static interface IInner{
		
	}
	
	static class Inner implements IInner{
		
	}
}

1.6. Example 6 a class with in method or block

public class MethodLocalOuterClass {
	
	// Inner class with in instance block
	{
		class InstanceBlockInner{
			
		}
	}
	
	// Inner class with in static block
	static {
		
		class StaticBlockInner{
				
		}
	 }
	
	// Inner class with in method
	public void test() {
		class MethodLocalInner{
			
		}
	}
}

1.7. Example 7 a name less class as argument to a method

public class AnonymousLocalExample {
	
	public static void main(String[] args) {
		
		String[] sarray = {"Peter", "Mike", "Zaheer", "John"};
		
		// anonymous class as method argument
		Arrays.sort(sarray, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return o2.compareTo(o1);
			}
		});
		
		System.out.println(Arrays.asList(sarray));
	}
}

Output :

[Zaheer, Peter, Mike, John]

2. Types of Nested classes

Based on the purpose and position of declaration all Java nested classes are divided into 4 types.

1. Normal or Regular inner classes (Non static)
2. Local Classes
3. Anonymous inner classes
4. Static nested classes

Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. Following image illustrates various types of nested classes in java.

java nested class types

3. Why do we use java nested class?

  1. It is a way of logically grouping classes that are only used in one place.
  2. It increases encapsulation.
  3. It can lead to more readable and maintainable code.
  4. Without existing one type of object if there is no chance of existing another type of object then we can go for inner classes.

4. Conclusion

In this article we have covered high level overview of Java nested class, what are the kinds of nested classes based on purpose and position of declaration with basic examples.

References

  1. Java documentation
- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles