Easy Tutorial
For Competitive Exams

Java Programming Static Keyword

What is static keyword?

The static keyword in java is used for memory management mainly.

We can apply java static keyword with variables, methods, blocks etc.,

The static keyword belongs to class than the instance .

The static can be:

  • variable (also known as class variable)
  • method (also known as class method)
  • block

What is static variable?

It is a variable which belongs to the class and not to object(instance).

Static variables are initialized only once , at the start of the execution.

These variables will be initialized first, before the initialization of any instance variables.

A single copy to be shared by all instances of the class.

A static variable can be accessed directly by the class name and doesn’t need any object.

Syntax

<class-name>.<variable-name>

Example:without static variable

class CounterVariable{
int count=0;
CounterVariable(){
count++;
System.out.println(count);
}
public static void main(String args[]){
CounterVariable c1=new CounterVariable();
CounterVariable c2=new CounterVariable();
CounterVariable c3=new CounterVariable();
  }
}
Try it Yourself

Example:with static variable

class CounterVariable{
static int count=0;
CounterVariable(){
count++;
System.out.println(count);
}
public static void main(String args[]){
CounterVariable c1=new CounterVariable();
CounterVariable c2=new CounterVariable();
CounterVariable c3=new CounterVariable();
  }
}
Try it Yourself

What is Static Method?

It is a method which belongs to the class and not to the object(instance).

A static method can access only static data. It can not access non-static data (instance variables).

A static method can be accessed directly by the class name and doesn’t need any object.

Syntax

<class-name>.<method-name>

Note:

  • A static method cannot refer to "this" or "super" keywords in anyway
  • main method is static , since it must be be accessible for an application to run , before any instantiation takes place.

Example

class Difference{
int a=10;
static void display() {
    System.out.println("Programming is amazing");
   }
  void show(){
    System.out.println("Java is awesome");
  }  
  public static void main(String[] args){
    display(); 
    Difference t = new Difference();
    t.show();
  }
  
}
Try it Yourself

What is static block?

Static block is mostly used for changing the default values of static variables.

This block gets executed when the class is loaded in the memory.

A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.

Example

class Example{
   
   static{
      System.out.println("This is block1");
   }
static{
      System.out.println("This is block2");
   }
   public static void main(String args[]){
      System.out.println("This is main method");
      
   }
}
Try it Yourself

What is Static import?

Normal imports will import the all the classes so that we can use them.

similarly static imports will import all static data so that can use without class name.

Static imports introduced in Java 5.

Example:Without Static Import

package easy;
 class StaticImport{
  public static void main(String [] args){
 
    System.out.println(Math.PI); 
    System.out.println(Integer.MAX_VALUE);
    System.out.println(Integer.parseInt("123"));
 
}
}
Try it Yourself

Example:With Static Import


package easy;
 
 import static java.lang.Integer.*;
 import static java.lang.Math.*;
 
class StaticImport{
  
public static void main(String [] args){
 
    System.out.println(PI); 
    System.out.println(MAX_VALUE);
    System.out.println(parseInt("123"));
 
}
}
Try it Yourself

Disadvantages of Static imports in java:

  • There may be a chance of complexity in readability.
  • If we use class name before method like Math.sqrt() then can understand easily that method belongs to particular class.But with static import reduces readability.
  • One more disadvantage is naming conflicts.

Solve this!!

14258.What is the output of the following program?
class MyClass {
     int a=10;
    static void display(){
        System.out.println(a);
    }
 
    public static void main(String[] args) {
        display();
    }
}
10
0
11
Error
Explanation:
static methods can not access the non-static members of a class. Error - non-static variable a cannot be referenced from a static context
14259.What is the output of this program?
class access{
       static int x=0;
       void increment(){
           x++;
       }   
     }   
    class static_use {
        public static void main(String args[]){
            access obj1 = new access();
            obj1.increment();
            obj1.increment();
            obj1.increment();
            System.out.println(obj1.x);
            }
   }
1
2
3
0
Share with Friends