Tuesday, September 24, 2019

The Ultimate Guide on Static Keyword In Java, 2019



Things to remember:
  Static is a keyword used for memory management
  Static means single copy storage for variable or method
  Static keyword can be applied to variables, methods, inner class and blocks
  Static members belongs to class rather than instance of class
           
Static in java is a keyword that indicates that the variables or functions are shared between all the instances of a particular class since it belongs to the type, not the object. It is used when the programmer wants to share the same variable or method of a class.
 
Static Variable:
  The variable preceded by ‘static’ keyword is ‘static variable’
     static int a=10;   //variable
     static void m1()
    {  
           // method
     }
  Static variable is used to refer common property of all objects of class

·         How to access static variable?
There are two ways to access static variable:
i)      Static variable can be accessed by Class name
        A. a ; [A is class name]
Where A is the class name and ‘a’ is a static variable declared in that class
ii)    Static variable can be accessed by object
    I have a class name called ‘Sample’. Now, we can create the object of the    Sample class
       Sample h=new Sample ();
   System.out.println(h.a);     //’a’ is static variable inside ‘sample’ class

·         How can I access static variable in two ways?

 See in the following program:

package com.javabykiran.Static;
/*
* @author Java By Kiran
*/
public class Staticvar {
static int i=10;
public static void main(String[] args) {
Staticvar s =new Staticvar();
System.out.println(s.i); //Not Recommended
System.out.println(Staticvar.i); //Recommended
}
}

 
 ---Output---
10
10
 
  In the above program, we printed the value of variable by using object name and by using class name
  Static variable gets loaded into the memory at the time of class loading
  So, we can access static variable by reference variables as well.
  In the above program, if we create only the reference of class like Staticvar s1=null;    System.out.println(s1.i);   // possible
System.out.println(Staticvar.i);  //possible
The above example compiles and executes successfully because the static variable get loaded into the memory at the time of class loading

  Static variable and method doesn't belong to Object/Instance of the class since static variables are shared across all the instances of Object

Example 1:

package com.jbk;
/*
* @author Java By Kiran
*/
public class StaticVar_Demo{
  int a =10;
  static int b =10;
public static void main(String[]args){

  StaticVar_Demo st= new StaticVar_Demo();    
  System.out.println(st.a);
   System.out.println(st.b);

StaticVar_Demo st1 = new StaticVar_Demo();

int x = st1.a++;
System.out.println(x);
int y = st1.b++;
System.out.println(y);
StaticVar_Demo st2 = new StaticVar_Demo();
int p = st2.a++;
System.out.println(p);
int q = st2.b++;
System.out.println(q);
StaticVar_Demo st3 = new StaticVar_Demo();
int c = st3.a++;
System.out.println(c);
int d = st3.b++;
System.out.println(d);
}
}

---Output---
10
10
10
10
10
11
10
12

About us: "Javabykiran" Provide the best python training in Pune. We have 12 years of experience in teaching in java training in Pune, we provide the best java classes in Pune. Our Python classes in Pune are trending in Pune. 

Follow us on:

For Video Tutorials: http://bit.ly/2mLWCxV

No comments:

Post a Comment

7 Reasons why you have to use python programming Language

1. Readable and Maintainable Code - While writing a code application, you need to concentrate  on the standard of its ASCII text file to...