Static keyword in java with examples

In this post, we will see about static keyword in java.So static keyword can be associated with:

  • Variable
  • Method
  • Block
  • Nested class

Lets go through each one by one.

Static variable:

If any instance variable is declared as static.It is known as static variable.

Some points about static variables are:
  • Static variable belongs to a class not to a object.
  • Static variable are initialized only once in class area at the time of class loading
  • All objects share single copy of static variable
  • You don’t need to create object to access static variable.You can directly access it using class name.
  • Static variable can be accessed by instance java methods also.

If you create a class with two variables, one static, one non static.Non static variable of that class (objects created from that class) gets its own version of that variable. But with the static variable, it belongs to the class, and there is only one. Although any object from that class can reference it.
Example:
Lets take a very simple example.You want to track how many objects you have created.For that you have a static variable in your class.Lets say your class is:
Country.java

Output:
When you run above program,you will get following results:

Diagram:

In above example,we have one static variable named “countryCounter” so whenever we are creating one new object,its value will be incremented by 1 as counterCounter variable is being shared by all country objects and we have instance variable named “dummyCounter” so whenever any new object is created,its value is initialized to 0 again.

Static Method:

If any method is declared as static.It is known as static method.

Some points about static methods are:
  •  Static method belongs to a class not to object.
  •  Static method can be invoked directly using a className.Although It can be invoked using      objectName also
  • Generally static method are used for getting static fields.
  • You can access static methods through java instance methods.
  • You can not access non static method or instance variable in static methods.
  • A static method can not refer to this or super keyword.
Example:
We will create above example and will use one static method printCountryCounter for printing countryCounter variable.
Country.java
Output:
When you run above program,you will get following results: 

Why can’t you access non static member from static method:

When you call non static method from static method,you will get compilation error.
For example:In above printCountryCounter(),Lets say you call getName()
You will compilation error.

 Imagine a situation, when you are allowed to call getName() from printCountryCounter() .You can call printCountryCounter() just using class name so when you call printCountryCounter() ,what value getName() should return because value may be different for each new object and which value will it refer.So that is why,you are not allowed call non static member from static method.

Static block:

The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM
Some points about static blocks are:
  • Static blocks are used to initialize static data members.
  • Static blocks get executed before main method get executed.
Output:
When you run above program,you will get following results:

Static class:

In java,you can define a class within another class. Such a class is called a nested class. The class which enclosed nested class is known as Outer class. In java, we can’t make outer class static.
Only nested class can be declared as static
Some points about nested static class are:
  • Nested static class does not need reference of outer class.
  • A static class cannot access non-static members of the Outer class. It can access only static members of Outer class including private.

Example:

Output:
When you run above program,you will get following results:

Related Posts

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 25 September

    Reference Variable in Java

    Table of ContentsReference variableStatic Reference VariableInstance Reference VariableLocal Reference Variable In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.