Static Members in Java class with example

In the previous tutorial, we learned about the Classes and Objects in Java. In this tutorial, we will learn about static members in java class.

997

As we know, when we create a Class in java, we define some variables in it, that will be used to store some values. These variables are also called Instance variables because they are originally created when the objects of these classes are created.

For example –

public class Example{
int x;// Instance variable
}

But if we use the static keyword before creating a variable, these kinds of variables are called static member variables in java.

For example –

public class Example{
static int y; // Static member variable
}

Similarly, if we define some functions inside a class, these functions are called instance member functions in java. These can be called only by creating the object of the class.

For example –

public static Example{
public void fun1(){  } // Instance member variable
}

But, if we use the static keyword before a class, this is called the static member function.

But, it is not possible to create a static local variable. This means we cannot create a static variable inside a function in a class.

For Example, if we write a code as below, it will generate an error.

public class Example{
public void func1(){
Static int a;}}

The following code will result in an error.

But, we can have a static inner class.

For example –

 Public class Example
Static class test{
}

So, In Java the following are possible –

  1. Static Member Variable
  2. Static Member Function
  3. Static Inner Class

Now, we will understand briefly the static variables.

What are Static Member Variables in Java

  • These are declared inside a class using the static keyword
  • Static variables initialize themselves to their default value
  • The static variable is created only once for the whole class, which does not depend upon the objects.

That means if we make multiple objects, still the static variables will be created only once and will be created even if we do not create any of the objects of the class.

We will understand how to call the static variable from the code below –

public class Example{
static int y;
public static void main(String args[]){
Example.y = 20;
}
}

In this, the variable y is assigned the value 20.

Now, we will learn about the Static functions

Static Member Functions in Java

  • Static functions are defined inside the class with the help of static keyword
  • They can only access members of the same class
  • They can be used by using the class names and dot operators.

We will understand about using the static member functions with the help of code given below –

Public class Example{
public static void main(String args[])
Example.x = 10;
}
}

So far, we learned about accessing the static variables and static member functions.

Static Inner Class

Now, we will learn about the Static Inner Class –

Creating a class within a class, and defining this class with the help of a static keyword is called static inner class.

We will learn how to access a static inner class with the help of the code given below –

Class Example{
Static class Inner{
Public static String name = “HARSHIT”;
}}

Public class Hello{
Public static void main(String args[]
{System.out.println (Example.Test.name);
}
}

We need to understand a concept here that static member functions can access only the static variables of their class. Because if we try to access the instance variables by the static member functions, there may be a possibility that the class’s object may not be created. In this case, there instance member function will try to access a variable that is not created. Which is a problem.

Also, if multiple objects of a class are created, then the static member variable will not understand the variable of which one object has to be used.

So, it is restricted to access the instance variables by the static member functions.

So, in this article, we learned about the static member functions, variables, and classes. Many a time, it becomes very useful to use this static keyword. For example – if we create an account class, the account holder’s name and account number can be created as instance variables because they will be different for different users. But, the rate of Interest can be created as a static variable because it will be the same and does not depend upon the users.

Related Quiz-

Previous QuizClasses and Objects in Java
Next QuizTaking input in Python

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.