Saturday, 3 March 2018

Stactic keyword in java

We can use static keyword with variables, methods, blocks and nested classes.
Static keyword is best for memory optimization in java.

Static Variable: We can access the static variables and methods using class without creating object for it.

Ex: Suppose we have one class and some variables

class Employee{
 int eNo;
 String eName;
 String orgName= "Java";
}

Here if we want to add employees, we need to create objects for very employee and these variables will be stored in memory every time for each object created. But here OrgName is common for all employees and it is not necessary to store every time. Hence if we use static keyword for this variable we can optimize the memory  because it is not stored for every object if it is static.

class Employee{
 int eNo;
 String eName;
 static String orgName= "Java"; //this is shared for all objects
}


package com.sample.javase.testing;

public class StaticVariableTest {

    int eNo;
    String eName;
    static String orgName = "java";
   
    StaticVariableTest(int eNo, String eName){
        this.eNo = eNo;
        this.eName = eName;
    }
   
    public void display(){
        System.out.println("emp no " + eNo + " name " + eName + " org name " + orgName);
    }
    public static void main(String[] args) {
       
        StaticVariableTest staticVariableTest1 = new StaticVariableTest(11, "test1");
        StaticVariableTest staticVariableTest2 = new StaticVariableTest(22, "test2");
        staticVariableTest1.display();
        staticVariableTest2.display();

    }

}

Output: emp no 11 name test1 org name java
emp no 22 name test2 org name java

Static Methods:

If we apply static keyword for any method, it is static method and we can access using class name.
And aslo static varibale can be access inside static method

package com.sample.javase.testing;

public class StaticVariableTest {

    int eNo;
    String eName;
    static String orgName = "java";
   
    StaticVariableTest(int eNo, String eName){
        this.eNo = eNo;
        this.eName = eName;
    }
   
    static void updateOrg(){
        orgName = "j2ee";
    }
    public void display(){
        System.out.println("emp no " + eNo + " name " + eName + " org name " + orgName);
    }
    public static void main(String[] args) {
       
        StaticVariableTest staticVariableTest1 = new StaticVariableTest(11, "test1");
        staticVariableTest1.display();
        StaticVariableTest.updateOrg();
        StaticVariableTest staticVariableTest2 = new StaticVariableTest(22, "test2");
        staticVariableTest2.display();
        System.out.println(staticVariableTest1.orgName);
        staticVariableTest1.updateOrg();//we can access the static variable using reference but not suggested
        staticVariableTest1.display();//we can access the static method using reference but not suggested

    }

}

Output:

emp no 11 name test1 org name java
emp no 22 name test2 org name j2ee
j2ee
emp no 11 name test1 org name j2ee


Understanding static:


package com.sample.javase.testing;

public class StaticVariableTest {

    int eNo;
    String eName;
    static String orgName = "java";
   
    StaticVariableTest(int eNo, String eName){
        this.eNo = eNo;
        this.eName = eName;
    }
   
    static void updateOrg(){
        //eName="testStatic";//we can not access non static inside static
        orgName = "j2ee";
//this.orgName = "j2ee";//this and sueper can not used inside static
    }
    public void display(){
        orgName = "spring";//we can access static in side non static but not suggested
        System.out.println("emp no " + eNo + " name " + eName + " org name " + orgName);
    }
    public static void main(String[] args) {
       
        StaticVariableTest staticVariableTest1 = new StaticVariableTest(11, "test1");
        staticVariableTest1.display();
        StaticVariableTest.updateOrg();
        StaticVariableTest staticVariableTest2 = new StaticVariableTest(22, "test2");
        staticVariableTest2.display();
        System.out.println(staticVariableTest1.orgName);
        staticVariableTest1.updateOrg();//we can access the static variable using reference but not suggested
        staticVariableTest1.display();//we can access the static method using reference but not suggested

    }

}


Output:

emp no 11 name test1 org name spring
emp no 22 name test2 org name spring
spring
emp no 11 name test1 org name spring



No comments:

Post a Comment