Static Nested class

Static Nested class

=================================

  • Sometime we can declare inner class with static modifier, such type of inner classes are called Static Nested class.
  • In the case of normal or regular inner class without existing outer class object there is not chance of existing inner class object. Inner class object strongly associated with outer class object.
  • But in the case of static nested classes without existing outer class object there may be a chance of existing nested class object.
  • Hence static nested class object is not strongly associated with outer class object. 

 

 

  • If we want to create nested class object from outside of outer class, then we can create as follow

            Outer.Inner i = new Outer.Inner(); 

  •  In normal or regular inner classes, we cannot declare any static members.
  • But in static nested classes we can declare static members including main method.
  • Hence we can invoke static nested class directly from command prompt.

 

 

  • From normal or regular inner classes we can access both static and non static members of outer class directly.
  • But from static nested classes we can access static members of outer class directly and we can't access non static members.

Ex.

 

 

Difference between normal or regular and static nested class

 Normal or Regular inner class

  • Without existing outer class object there is no chance of existing inner class object. Means inner class object is strongly associated with outer class object.
  • In this classes we can't declare static members.
  • In this classes we can't declare  main method and hence we can't this class directly from command prompt.
  • From this classes we can access both static or not static members of outer class directly.

 Static Nested class

  • Without existing outer class object there may be a chance of existing static nested class object. Hence static nested class object is not strongly associated with outer class object.
  • In this classes we can declare static method.
  • In this classes we can declare main method and hence we can invoke this class directly from command prompt.
  • From this classes we can access only static members of outer class.
 
 

Various combination of Nested classes and Interfaces

 Case 1.

Class inside a class :-  Without existing one type of object is there is no chance of existing another type of object, then we can declare class inside a class.

 Ex.

University consist of several department . Without existing university, there is no chance of     existing department. Hence we have to declare Department class inside university class.

class University
{
     class Department
    {
 
    }
}

Case 2.

Interface inside a class :-  Inside a class if we require multiple implementation of an interface and all these implementation are related to a particular class, then we can define interface inside a class.

Ex.
class VehicalType
{
        interface  Vehical
        {
            public int getNumberOfWheel();
        }

        class Bus implements Vehical
        {
            public int getNumberOfWheel()
            {
                return 6;
            }
        }
      
       class Auto implements Vehical 
      {
public int getNumberOfWheel()
{
     return 3;
}
      }
    .
    .
    .
    .
}

Case 3.

Interface inside interface :- We can declare interface inside interface.
  
Ex.
 
A map is a group of Key,Value pairs and each Key,Value pair is called an Entry. 
Without existing Map object there is no chance of existing Entry object. 
Hence interface Entry is defined inside Map interface.
 


Every interface present inside interface is always public and static whether we are declaring or not. 
Hence we can implement inner interface directly without implementing outer interface. 
Similarly when ever we are implementing outer interface we are not required to implement inner interface.  
Means we can implement outer and inner interface independently.



 
 
 

 Case 4.

Class inside interface :- If functionality of a class is closely associated with interface then it is highly  recommended to declare a class inside interface.

interface EmailService
{
    public void sendEmails(EmailDetails e);
      
     class EmailDetails
    {
        String toList;
        String ccList;
    }
 
}
 
 
In the above example EmailDetails is required for EmailServices and we are not using any where else.
Hence EmailDetails class is recommended to declare inside EmailService interface.

We can also implement/ define a class inside interface to provide default implementation for that interface.

Ex.
  

In the above example DefaultVehicle  is the default implementation of Vehicle interface.
Where as Bus is customized implementation of Vehicle interface.

Note:-

  • The class which is declare inside interface is always public and static whether we  are declaring or not.
  • Hence we can create class object directly without having outer interface type object.

1. Among classes and interface we can declare any thing inside any thing.


2. The interface which is declare inside interface is always public and static whether we are declaring or not.

3. The class which is declare inside interface is always public and static whether we are declaring or not.

4. The interface which is declared inside a class is always static but need not be public.

 

 

Related topic

RAKESH RAKA

I am Rakesh Raka, senior software engineer (JAVA) in Sopra Steria.

Post a Comment (0)
Previous Post Next Post