Static

 

//A class with Static methods and Static Variables

 

public class StaticClass {

    //The static variable and static method belongs to class and not to object
    private static int intVariable = 300;


    //you can only call static Member variables and static functions
    // with in another static function
    //Static Method
    public static void staticMethod(){
        System.out.println(intVariable);
        printStatic();
    }

    //Static Method called in another static method
    public static void printStatic(){
        System.out.println("Print Static");
    }

    //Normal Method cannot be called under static method
    public  void printNormal(){
        System.out.println("Print Normal");
    }
}

 

 

 

//Main Method
public class MainClass {


    public static void main(String[] args) {
        //Calling non static functions using an object
        StaticClass staticClass = new StaticClass();
        staticClass.printNormal();

        //Calling static methods
        StaticClass.printStatic();
        StaticClass.staticMethod();

    }
}

 

 

Print Print | Sitemap
© 2016 Automation Learn. All rights reserved.