Can We Create Object Of Static Class In Java Code Example

Snippet 1
  // Java program to demonstrate how to 
// implement static and non-static 
// classes in a Java program. 
class OuterClass { 
    private static String msg = "GeeksForGeeks"; 
    // Static nested class 
    public static class NestedStaticClass { 
        // Only static members of Outer class 
        // is directly accessible in nested 
        // static class 
        public void printMessage() 
            // Try making 'message' a non-static 
            // variable, there will be compiler error 
                "Message from nested static class: "
                + msg); 
    // Non-static nested class - 
    // also called Inner class 
    public class InnerClass { 
        // Both static and non-static members 
        // of Outer class are accessible in 
        // this Inner class 
        public void display() 
                "Message from non-static nested class: "
                + msg); 
class Main { 
    // How to create instance of static 
    // and non static nested class? 
    public static void main(String args[]) 
        // Create instance of nested Static class 
        OuterClass.NestedStaticClass printer 
            = new OuterClass.NestedStaticClass(); 
        // Call non static method of nested 
        // static class 
        // In order to create instance of 
        // Inner class we need an Outer class 
        // instance. Let us create Outer class 
        // instance for creating 
        // non-static nested class 
        OuterClass outer = new OuterClass(); 
        OuterClass.InnerClass inner 
            = InnerClass(); 
        // Calling non-static method of Inner class 
        // We can also combine above steps in one 
        // step to create instance of Inner class 
        OuterClass.InnerClass innerObject 
            = new OuterClass().new InnerClass(); 
        // Similarly we can now call Inner class method 

Similar Snippets

Create Copy Of Array From Another Array Code Example - java

Js How To Prevent A Code From Runing Code Example - java

Array Fot String Code Example - java

While Loop Continue Js Code Example - java

Find Duplicates In Arraylist Java Code Example - java

For Each Javascript Code Example - java

Java Creat A Folder Code Example - java

Java Get Random Index From Array Code Example - java

How To Round A Number Javascript Code Example - java

Firestore Find Doc And Set Data Code Example - java

Java How To Get Files In Resources Code Example - java

Remove Last Letter From String Java Code Example - java

Simpledateformat Example Java Code Example - java

Different Types Of Writing If Else Statements Jav Code Example - java

How To Take A Image As A Background In Tkinter In Python Code Example - java

Creating Java Main Method Code Example - java

Switch Case Accepts Byte In Java Code Example - java

Find Duplicate And Repeating Number In Array Code Example - java