Thursday, 25 June 2015

staticVariable and Non static program

Static variable 

The value which is stored in the variable that does not change


package Javaprgs;

public class Staticmem {
 static double a = 34.5;//global variable
    public static void main(String[] args) {
        double a = 43.6;//local variable
        int b=12;
        System.out.println("program starts");
        System.out.println("local variable a- "+a);
        System.out.println("global variable a- "+Staticmem.a);
        staticMethod();//call the staticmethod
        System.out.println("Program ends.");
    }
    public static void staticMethod(){
        System.out.println("static method.");
    }

}

Non Static Variable

package Javaprgs;

public class Nonstaticvar {
 int nonStaticVar=2;
    public static void main(String[] args) {
        System.out.println("program starts");
        
        Nonstaticvar refVarName1 = new Nonstaticvar();  //this is creating the object1
        Nonstaticvar refVarName2 = new Nonstaticvar();
        System.out.println("ref1method");
        //this is creating the object2
        System.out.println(refVarName1.nonStaticVar);
        System.out.println(refVarName2.nonStaticVar);
        System.out.println("Program ends.");
    }

}



No comments:

Post a Comment