Static Keyword -Java

Static Keyword -Java

What is a static keyword?

The static keyword is basically a non-access modifier

We can use it with class-level variable

We can use methods

We can make a static block

We can use static with inner class too

Why do we create static variables?

static variables' main use is for memory management when a variable is declared as static then a single copy of the variable is created and shared among all the objects at the class level.

 We create static variables when we want to make our program memory efficient

And when we have some variable common for all the objects, then we make that object static

No alt text provided for this image
No alt text provided for this image

Static Methods

Static methods belong to class not object

Static methods can be called directly by class name

No alt text provided for this image

Static Block

 What is a static block?

The static block gets executed automatically when a class is loaded in the memory

 We need a main method to execute a static block in java1.8

But in java version 1.6 and older we can execute static block without main method

No alt text provided for this image


To view or add a comment, sign in

More articles by Vandana Yadav

  • Access Modifier's in Java

    What are Access Modifiers? In object oriented programming world ,the concept of modifier and their behaviour is one of…

    1 Comment
  • Interface vs Abstract class

    1- Abstraction is hiding internal implementation and highlighting the setup services that we are offering 2-…

    1 Comment
  • Exception Handling in Java

    What is an exception? What is an exception? An exception occurs when there is a statement whose output JVM is not able…

  • Constructor's in Java part -1

    Let try to understand constructors in java by answering few questions What is a constructor? 1-Constructor is a block…

    1 Comment
  • Reflection API -Java

    Reflection is an API that is used to get information on the behaviour of methods, classes, interfaces at runtime. This…

    2 Comments
  • What is the collection framework

    As we all know array stores elements of the same data type and the drawbacks it has is that it cannot support dynamic…

    2 Comments
  • Pojo Notes

    Understanding POJO and its conversion What is POJO P- Plain O-old J-java O- object Class in java is nothing but…

    1 Comment

Explore content categories