Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

static class java

// Java program to Demonstrate How to
// Implement Static and Non-static Classes
 
// Class 1
// Helepr class
class OuterClass {
 
    // Input string
    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
            System.out.println(
                "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()
        {
 
            // Print statement whenever this method is
            // called
            System.out.println(
                "Message from non-static nested class: "
                + msg);
        }
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating instance of nested Static class
        // inside main() method
        OuterClass.NestedStaticClass printer
            = new OuterClass.NestedStaticClass();
 
        // Calling non-static method of nested
        // static class
        printer.printMessage();
 
        // Note: In order to create instance of Inner class
        //  we need an Outer class instance
 
        // Creating Outer class instance for creating
        // non-static nested class
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner
            = outer.new InnerClass();
 
        // Calling non-static method of Inner class
        inner.display();
 
        // We can also combine above steps in one
        // step to create instance of Inner class
        OuterClass.InnerClass innerObject
            = new OuterClass().new InnerClass();
 
        // Similarly calling inner class defined method
        innerObject.display();
    }
}
Comment

static class java


public class TestMyStaticClass {
     public static void main(String []args){
        MyStaticClass.setMyStaticMember(5);
        System.out.println("Static value: " + MyStaticClass.getMyStaticMember());
        System.out.println("Value squared: " + MyStaticClass.squareMyStaticMember());
        // MyStaticClass x = new MyStaticClass(); // results in compile time error
     }
}

// A top-level Java class mimicking static class behavior
public final class MyStaticClass {
    private MyStaticClass () { // private constructor
        myStaticMember = 1;
    }
    private static int myStaticMember;
    public static void setMyStaticMember(int val) {
        myStaticMember = val;
    }
    public static int getMyStaticMember() {
        return myStaticMember;
    }
    public static int squareMyStaticMember() {
        return myStaticMember * myStaticMember;
    }
}

Comment

PREVIOUS NEXT
Code Example
Java :: regex double quote java 
Java :: initialize hashmap java 
Java :: Java Exception handling using Java throw 
Java :: jasypt-spring-boot 
Java :: is it possible to quick sort a string in java 
Java :: java multithreading 
Java :: java queue implementation 
Java :: how to get index of arraylist in java 
Java :: Hash map, Hash set and Hash Table difference 
Java :: java max value between two numbers 
Java :: get all enum values java 
Java :: deserialize list jackson 
Java :: how to get string input line in java 
Java :: android convert date to local timezone 
Java :: traversing treemap in java 
Java :: R8: java.lang.OutOfMemoryError: GC overhead limit exceeded react-native 
Java :: what is constructor in java 
Java :: mongodb check if field exists java 
Java :: MyLinkedList 
Java :: java sort arraylist of floats 
Java :: mvn spring boot ends when closing vm 
Java :: java calcuate milliseconds since 1970 
Java :: Exception in thread "main" java.lang.NoClassDefFoundError sdkmanager 
Java :: java create an instance of a stack 
Java :: arraylist<hashmap<string, string arraylist = new arraylist<() 
Java :: arraylist to primitive array 
Java :: JFrame change border 
Java :: java string reduce 
Java :: pythagoras method in java 
Java :: android snackbar message is behind back button 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =