JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Java static keyword

In Java, static keyword is mainly used for memory management. It can be used with variables, blocks, methods and nested classes. It is a keyword that is used to share the same variable or method of a given class. When a member is declared static, then it essentially means that the member is shared by all the instances of a class without making copies of per instance.

In Simple words, It is a keyword used to define class member.

Thus static is a non-class modifier in Java and can be applied to the following members:

1. blocks

2. variables

3. methods

4. nested classes

1. Static Blocks

In Java, static blocks are used to initialize the static variables/data member.

In java static block is executed at the time when the first object of the class is created (precisely at the time of classloading) or when the static member inside the block is used.

Example of static block

class Simple {

// static variables
static int a = 20;
static int b;
static int min;

// static blocks
static {
    System.out.println("First Static block.");
    b = a * 2;
}
static {
    System.out.println("Second Static block.");
    min = 25;
}

// static method
static void display() {

    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("min = " + min);
}

public static void main(String args[]) {
    // calling the static method
    display();
}
}

Output:

First Static block.
Second Static block.
a = 20
b = 40
min = 25

Static Variable

In java a member variable of a class that is declared as static is called the Static Variable. It is also called as the Class variable.

When a variable is declared as static, then a single copy of static variable is created and shared by all the objects.

Example of static variable

class CollegeStudent
    {  
        int rollno; //instance variable  
        String name;  
        static String college ="Xyz";//static variable  

    //constructor  
    CollegeStudent(int r, String n)
    {  
        rollno = r;  
        name = n;  
    }  

    //method to display the values 

    void display ()
    {
        System.out.println(rollno+" "+name+" "+college);
    }  
}  
  
class Simple{  
    public static void main(String args[])
    {  
        CollegeStudent stu1 = new CollegeStudent(101,"Peter");  
        CollegeStudent stu2 = new CollegeStudent(202,"Tony Stark");  
 
        stu1.display();  
        stu2.display();  
    }  
}  

Output:

101 Peter Xyz
202 Tony Stark Xyz

Static methods

In java when a method is declared with static keyword, it is known as static method. static methods also belong to a class instead of the object, and so they can be called without creating the object of the class in which they reside.

Some points related to static method:

1. A static method cannot refer to non-static data members or variables and cannot call non-static methods too.

2. The static method can also call other static methods.

3. A static method cannot have a reference to this or super members.

4. The static method can even change the values of the static data member.

Example of static method:

class Simple
{
    // static method
static void sMethod()
    {
        System.out.println("Hyy i'm static method in Java...");
    }
    
public static void main(String[] args)
    {
        sMethod();
    }
}

Output:

Hyy i'm static method in Java...

Example 2: static method

class Cal {

    
    // static method
    static int multiply(int a, int b){
        return a * b;
    }
}

class Simple {

    public static void main( String[] args ) {

        // create an instance of the Cal class
        Cal st = new Cal();

        // call the static method
        System.out.println("2 * 5 = " + Cal.multiply(2,5));
    }
}

Output:

2 * 5 = 10

Nested Static Class

In Java, we can declare a class inside another class. Such classes are known as nested classes.

Nested classes are 2 types:

1. Static Nested Classes

2. Non-static Nested Classes

Nested static class doesn’t need a reference of Outer class. In this case, a static class cannot access non-static members of the Outer class.