Static Member and Scope in Java

Static Keyword in Java

Static Member and Scope in Java is used to increase the memory efficiency by utilizing less space. As a static variable can be accessed by any member of a class without initializing again.

The Static could be:

  1. Variable
  2. Method
  3. Block
javastatic member and scope

Static Member and Scope in Java

Static Variable in Java

  • A static variable’s value is set when it is defined, and we do not need to make an instance of it in order to utilise it.
  • Memory is only allocated once because the variable is only created once.
  • Static variables in Java are stored in the heap memory

Add Your Heading Text Here

Static Variable

Run

class emp {
    String Employee_id;//This is an Instance Variable
    String Name;//This is an Instance Variable
    static String Company = "PrepInsta";

    emp(String id, String name) {
        Employee_id = id;
        Name = name;
    }

    void display() {
        System.out.println(Employee_id + " " + Name + " " + Company);
    }
}

public class Main {
    public static void main(String args[]) {
        emp s1 = new emp("P01", "XYZ");
        emp s2 = new emp("P02", "ABC");
        s1.display();
        s2.display();
    }
}

Static Methods in Java

  • Only static variables can be accessed and modified by static methods.
  • It is possible to call or use static methods without first creating a class instance.

Static Method

Run
class emp {
    String Employee_id;
    String Name;
    static String Company = "PrepInsta";//Static Variable</pre>
    emp(String id, String name) {
        Employee_id = id;
        Name = name;
    }

    void display() {
        System.out.println(Employee_id + " " + Name + " " + Company);
    }

    static void Change() {//Static Method
        Company = "PrepInsta Prime";
    }
}

public class Main {
    public static void main(String args[]) {
        emp e1 = new emp("P01", "XYZ");
        emp e2 = new emp("P02", "ABC");
        System.out.println("Before\n");
        e1.display();
        e2.display();
        emp.Change();
        System.out.println("\nAfter\n");
        e1.display();
        e2.display();
    }
}

Static Block in Java

When a Java class is first loaded into the JVM, a block of statements known as the static block is executed. Similar to how constructors aid in initialising instance members, a static block aids in initialising the members of static data.

Static Block

Run
class Main{
    static{
        System.out.println("Static Block Executed by default");
    }
    public static void main(String args[]){
        System.out.println("PrepInsta");
    }
}

Scope of Static Variable

  • Static variables have a single copy that can be shared by all of the class’s objects and are allocated memory only once. Static variables improve the program’s memory efficiency as a result.
  • Unlike static variables, which only have one copy made for a class, instance variables create independent copies for each object.
  • Hence, When a class is loaded, static variables are created, and when a class is unloaded, they are destroyed. Therefore, the scope of the static variable and the class are same.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription