Nested Classes in Java

Nested Classes - Exercise

Nested Classes (Static)

Introduction to Nested Classes in Java

Nested classes in Java are classes that are declared inside another class. They can be either static or non-static. Static nested classes are declared with the `static` keyword, while non-static nested classes are not.

Nested classes can be used for a variety of purposes, such as:

  • To encapsulate related functionality together.
  • To create inner classes that have access to the private members of the outer class.
  • To create local classes that are only needed within a particular scope.

Here is an example of a nested class:

public class OuterClass {
  private int x = 10;

  public class InnerClass {
    public void accessOuterMember() {
      System.out.println(x);
    }
  }

  public static void main(String[] args) {
    OuterClass outerClass = new OuterClass();
    OuterClass.InnerClass innerClass = outerClass.new InnerClass();
    innerClass.accessOuterMember();
  }
}

In this example, the `InnerClass` class is a nested class of the `OuterClass` class. The `InnerClass` class has access to the private `x` member of the `OuterClass` class.

Nested classes can be a powerful tool for organizing and structuring your code. However, it is important to use them carefully, as overuse of nested classes can make your code difficult to read and maintain.

Here are some tips for using nested classes effectively:

  • Use nested classes to encapsulate related functionality together.
  • Use nested classes to create inner classes that have access to the private members of the outer class.
  • Use nested classes to create local classes that are only needed within a particular scope.
  • Be careful not to overuse nested classes, as this can make your code difficult to read and maintain.
  • Document the reasons for using nested classes in your code.

If you are working with Java, I encourage you to learn more about nested classes and how to use them effectively.

Exercises

Demo Code for declaration and Instantiation of a Nested Class in Java


package practiceproject;
import java.util.*;
public class democlass {
  public static void main(String[] args){	
    outer obj = new outer();
    outer.nested nestedobj = obj.new nested();
    nestedobj.display();	
  }
}

class outer
{
  public String var1;
  private String var2;
  protected String var3;
  public static int count=1;
  public outer() {
    var1="test1";
    var2="test2";
    var3="test3";
  }
  class nested
  {
    public void display() {
      
      System.out.println(var1+var2+var3+count);
    }
  }
} 

Demo Code for working with an array of objects for a nested class.


  package practiceproject;
  import java.util.*;
  public class democlass {
    
    public static void main(String[] args){	
      //A faculty member can have multiple degrees . You should be able to print 
      //the faculty name along with the list of degrees that has been awarded to him		
      //create an object for faculty
      faculty f1 = new faculty("satish","222");
      //create an object for the nested class degree
      faculty.degree deobj[]= new faculty.degree[2];
      Scanner input = new Scanner(System.in);
      for(int i=0;i<deobj.length;i++)
      {
            System.out.println("Enter the degreename,year and awardedby");
            String dname=input.nextLine();
            String iyear= input.nextLine();
            String awardedby=input.nextLine();
            deobj[i]=f1.new degree(dname,iyear,awardedby);
            
      }
      f1.display_facultydetails(deobj);
    }
  }
  
  class faculty
  {
    private String name;
    private String empid;
    
    public faculty(String iname,String iemp) {
      name=iname;
      empid=iemp;
    }
    
    public class degree
    {
      //data members for degree class
      public String degree_name;
      public String awarded_year;
      public String awardedby;
      
      public degree(String idname,String iyear,String iawardedby) {
        degree_name=idname;
        awarded_year=iyear;
        awardedby=iawardedby;
      }
      
      /*public void display_facultydetails() {
        
        System.out.println("The faculty name is"+name );
        System.out.println("The employee id is "+empid);
        
        System.out.println(degree_name+awarded_year+awardedby);
      }*/
      
    }
    public void display_facultydetails(degree d[])
    {
      System.out.println("The name of the faculty is "+name);
      System.out.println("The degree details are");
      for(degree k:d)
      {
        System.out.println(k.degree_name+k.awarded_year+k.awardedby);
      }
    }
  } 

Demo Code for Static Nested Classes


  package practiceproject;
  import java.util.*;
  public class democlass {
    
    public static void main(String[] args){			
      //creating an object of type faculty
      faculty f1 = new faculty("Satish", "222");
      faculty.eligible_leave.display_leavetypes();
      faculty f2 = new faculty("ram","223");
      faculty.eligible_leave.display_leavetypes();
      f2.e1.display_test();
    }
  }
  
  class faculty
  {
    private String name;
    private String empid;
    private static int count=1;
      public faculty(String iname,String iemp) {
        this.name=iname;
        this.empid=iemp;
      }	
    public static class eligible_leave
    {
      static int encashed_leave=12;
      static int medical_leave=20;
      static int casual_leave=10;
      private int count1;
      public eligible_leave() {
        count1++;
      }
      public static void display_leavetypes() {
        System.out.println("The total encashed leave is"+ encashed_leave);
        System.out.println("The total medical leave is "+ medical_leave);
        System.out.println("The total casual leave is"+ casual_leave);
        System.out.println(count);
      }
      
      public void display_test()
      {
        System.out.println("hello this is working"+ count1);
      }
      
    }
    eligible_leave e1 = new eligible_leave();
  
  }