The use of Java static keyword

Java static keyword uses

The primary use of static keyword in java is memory management.Static keyword can  be used in

  • Variables
  • Methods
  • blocks
  • nested  classes.
1.Java static variable
Java static variable gets memory once.This means that the value once assigned does not change.Static variable can be used only to refer a common property with class objects.
Example.


/**
 *
 * @author Eric
 * Blog:www.techoverload.net
 */
public class StudentClass {
    int id;
    String name;
    static String className="BIT2016";
    
    StudentClass(int idNo,String n)
    {
    id=idNo;
    name=n;
    }
    
    void showDetails()
    {
      System.out.println("=========Student Details======");
    System.out.println("Student ID "+id);
      System.out.println("Student Name "+name);
        System.out.println("Student Class"+className);
      System.out.println("===============================");
    }
    public static void main(String args[])
    {
    StudentClass c=new StudentClass(1112,"Eric");
      StudentClass c2=new StudentClass(1113,"Jane");
      c.showDetails();
      c2.showDetails();
    }
}

Counter program without static variable

Below code demonstrate how we loose the value of counter variable because it has not been declared static.This is because the variable get assigned a value each time a new object is created.

Example

/**
 *
 * @author Eric
 * Blog:www.techoverload.net
 */
public class CounterClass {
    int count=0;
    CounterClass()
    {
    count++;
    
    System.out.println("The count is " +count);
    }
    public static void main(String args[])
    {
    CounterClass c=new CounterClass();
       CounterClass c2=new CounterClass();
          CounterClass c3=new CounterClass();
    
    }
    
}

Output

The count is 1
The count is 1
The count is 1

Counter program with static variable

/**
 *
 * @author Eric
 * Blog:www.techoverload.net
 */
public class CounterClass {
    static int count=0;
    CounterClass()
    {
    count++;
    
    System.out.println("The count is " +count);
    }
    public static void main(String args[])
    {
    CounterClass c=new CounterClass();
       CounterClass c2=new CounterClass();
          CounterClass c3=new CounterClass();
    
    }
    
}


Output:

The count is 1
The count is 2
The count is 3

2.Java static method

If static keyword is used with any method,it becomes a static method.

Static method xstics

  • static method belongs to the class and not the object.
  • static method can be called without creating class instance.
  • static method can access static data and change them.

Example

Below code shows a static method that has access to static variable and changes it.
/**
 *
 * @author Eric
 * Blog:www.techoverload.net
 */
public class StudentClass {
    int id;
    String name;
    static String className="BIT2016";
    
    StudentClass(int idNo,String n)
    {
    id=idNo;
    name=n;
    }
    
    static void changeClass()
    {
    className="BIT2017";
    }
    
    void showDetails()
    {
      System.out.println("=========Student Details======");
    System.out.println("Student ID "+id);
      System.out.println("Student Name "+name);
        System.out.println("Student Class"+className);
      System.out.println("===============================");
    }
    public static void main(String args[])
    {
    StudentClass c=new StudentClass(1112,"Eric");
    //calling static method
      StudentClass.changeClass();
      StudentClass c2=new StudentClass(1113,"Jane");
      c.showDetails();
      c2.showDetails();
      
    }
}



Example of static method to do calculations

/**
 *
 * @author Eric
 * Blog:www.techoverload.net
 */
public class Calculation {
    static int calc(int p)
    {
        return p*p*p;
    }
    public static void main(String args[])
    {
    int rs=Calculation.calc(20);
    System.out.println("The result is "+rs);
    }
    
}



Note that

  • static method cannot use non static data member or call non static method.
  • this and super cannot be used in static context.

3.Java static block

The main use  of static block is :-

Initialize static data member.
Executed before main method.

public class A2{  
  static{System.out.println("static block is invoked");}  
  public static void main(String args[]){  
   System.out.println("Hello main");  
  }  
}

Related Articles:

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: The use of Java static keyword
The use of Java static keyword
Java static keyword uses
Java Codes
http://www.download-all.net/2017/04/java-static-keyword-uses.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/04/java-static-keyword-uses.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy