static keyword in Java

  1. Java static variable

    We can use static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class.
    Since static variables are shared across all the instances of Object, they are not thread safe.
    Usually, static variables are used with the final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it with ClassName.variableName
    
        //static variable example
        private static int count;
        public static String str;
        public static final String DB_USER = "myuser";
    
  2. Java static method

    Same as static variable, static method belong to class and not to class instances.
    A static method can access only static variables of class and invoke only static methods of the class.
    Usually, static methods are utility methods that we want to expose to be used by other classes without the need of creating an instance. For example Collections class.
    Java Wrapper classes and utility classes contains a lot of static methods. The main() method that is the entry point of a java program itself is a static method.
    
        //static method example
        public static void setCount(int count) {
            if(count > 0)
            StaticExample.count = count;
        }
        
        //static util method
        public static int addInts(int i, int...js){
            int sum=i;
            for(int x : js) sum+=x;
            return sum;
        }
    

  3. Java static block:   Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader.

    Static block is used to initialize the static variables of the class. Mostly it’s used to create static resources when the class is loaded.
    We can’t access non-static variables in the static block. We can have multiple static blocks in a class, although it doesn’t make much sense. Static block code is executed only once when the class is loaded into memory.
    
        static{
            //can be used to initialize resources when class is loaded
            System.out.println("StaticExample static block");
            //can access only static variables and methods
            str="Test";
            setCount(2);
        }
    
  4. Java Static Class

    We can use static keyword with nested classes. static keyword can’t be used with top-level classes.
    A static nested class is same as any other top-level class and is nested for only packaging convenience.
  5. Java static import
Normally we access static members using Class reference, from Java 1.5 we can use java static import to avoid class reference. Below is a simple example of Java static import.

package com.practice.test;

public class A {

	public static int MAX = 1000;
	
	public static void foo(){
		System.out.println("foo static method");
	}
}

package com.practice.test;

import static com.practice.test.A.MAX;
import static com.practice.test.A.foo;

public class B {

	public static void main(String args[]){
		System.out.println(MAX); //normally A.MAX
		foo(); // normally A.foo()
	}
}
Notice the import statements, for static import we have to use import static followed by the fully classified static member of a class. 

Comments

Popular posts from this blog

gsutil Vs Storage Transfer Service Vs Transfer Appliance

SQL basic interview question