September 01, 2022 |4.1K Views

    Static Variable, Static Method & Static Block in Java

    Description
    Discussion

    For memory management, we mainly use the static keyword in Java.
    In order to share the same variable or method we use the keyword static.

    The static keyword is a non-access modifier in Java that is applicable for the following: 

    1. Blocks
    2. Variables
    3. Methods
    4. Classes

    Static Blocks:
    1) A class can have multiple static blocks
    2) The static block gets executed when class is loaded in the memory
    3) To initialise the static variable, we use the static block

    Static Variables:
    1) It is a class level variable
    2) It's memory allocation takes place when the class is loaded in the memory
    3) Also known as class variable

    Static Methods:
    1) They can access the static variables/class variables without using objects

    Syntax:
    static return_type method_name();

    Watch the complete video to understand the differences between these three with examples

    Static keyword in Java: https://www.geeksforgeeks.org/static-keyword-java/