Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

November 26, 2014 Categories: Core Java Basics. No Comments on static Keyword

In this article we will look at static keyword which can be used to share data among multiple objects.

 

static Keyword

In Java programs, static keyword can be used to create the following:

  1. Class variables
  2. Class methods
  3. Static blocks

 

Class Variables:

The static keyword is used to create one of the three types of variables called as class variables. A class variable is a variable declared inside a class and outside all the methods and is marked as static. Syntax for declaring a class variable or a static variable is shown below:

static data-type variable-name;

Example for declaring a class variable is shown below:

static int id;

What is special about a class variable? As you already know, an instance variable is created separately for every object of the class. But a class variable is created only once inside the memory and the same is shared among all the objects of a class. Consider the following code segment to demonstrate the difference between instance variables and class variables.

Below figure illustrates the difference between an instance variable and a class variable in the above code segment:

Instance variables vs static variables

Class Methods: 

All the non-static methods inside a class are known as instance methods and all the static methods inside a class are known as class methods. A class method is generally used to process class variables. A class method has the following limitations:

  1. A class method (static method) can access only other class methods.
  2. A class method can access only class variables (static variables).
  3. this and super cannot be used in class methods.

A class method can be created using the following syntax:

An example for creating a class method is shown below:

Following code segment illustrates the use of class methods in Java:

 

Static Blocks:

A static block is a block of statements prefixed with static keyword. The syntax for creating a static block is shown below:

An important property of a static block is, the statements in a static block are executed as soon as the class is loaded into memory even before the main method starts its execution. A typical use of static blocks is initializing the class variables.

Following program demonstrates the use of static blocks:

Note: Another important property of static keyword is, any class variable (static variable) or a class method (static method) can be accessed directly without creating an object for the class using the following syntax:

ClassName.variablename;

or

ClassName.methodname( );

This is the reason why the main method is declared as static in every program so that the JVM can access it directly without creating an object for the class. Following program demonstrates the alternative syntax for accessing class variables and class methods:

 

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me