Thursday , August 13 2020
Home / Java / JAVA #23 – Nested Classes and Static Keyword in Java

JAVA #23 – Nested Classes and Static Keyword in Java

Nested Classes in Java

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

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.

Syntax:

public class Test {

   public void class1() {
        . . .
   }
   public void class2() {
        .. .
   }
}
public class Testing {
   public static void main(String[] args) {
        . . .
   }
}
// File name NestedClasses.java 
package nestedclasses;
public class NestedClasses {
    public static void main(String[] args) {
        Testing T = new Testing(); // Outter Class
        Testing.Test1 T1 = T.new Test1(); // Nested Object Of An Class
        T1.Dis();
        
        Testing.Test2 T2 = new Testing.Test2();
        T2.Dis();
        
        int a = Testing.i;
        System.out.println(a);
        Testing.Display("Ali");
    }
}
// File name Testing.java must be store in same package of above class.
package nestedclasses;
public class Testing {    
    public static void Display(String name)
    {
        System.out.println("The Name Is: "+name);
    }   
    public static int i = 10;
        public class Test1{
             public void Dis()
            {
                System.out.println("Test1 Class");
            }
        }       
        public static class Test2{
             public void Dis()
            {
                System.out.println("Test2 Class");
            }
       }
}

 

 

About Faheem

An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #32 – Java application using SQL server (Part: 01)

  In this part we made the GUI front end of a Application.

Leave a Reply

Your email address will not be published.