Static Keyword in Java

 In Java, the static keyword is mostly used for memory management. With variables, methods, blocks, and nested classes, we may use the static keyword. The static keyword refers to a class instead of a specific instance of that class. this means if you make a member static, you can access it without object.

Here we have a static method TestMethod(), we can call this method without any object because when we make a member static it becomes class level. If we remove the static keyword and make it non-static then we must need to create an object of the class in order to call it.

Static Keyword in Java
static keyword


Example :

class SimpleStaticClass 
{
// This is a static method
static void TestMethod()
{
System.out.println("hello");
}

public static void main(String[] args)
{
/* You can see that we are calling this
* method without creating any object.
*/
myMethod();
}
}

Outpu :
hello




{tocify} $title={Table of Contents}


 


static variable :


if we declare any variable as static these variable is known as static variable. only a single copy of static variable is created and shared among all the instances of the class. The static variable gets memory only once in the class area at the time of class loading.

  • Static variables are also known as Class Variables.
  • Such variables, unlike non-static variables, may be accessed directly in both static and non-static methods.
Example 1  :

class Test
{
static int a=10;
static String str="test";
//This is a Static Method
static void show()
{
System.out.println("int is: " + a);
System.out.println("String is : " + str);
}
public static void main(String args[])
{
show();
}
}

OutPut :
int is : 10
String is : test


Example 2 :

class Demo
{
static int count=0;
public void increment()
{
count++;
}
public static void main(String args[])
{
Demo obj1=new Demo();
Demo obj2=new Demo();
obj1.increment();
obj2.increment();
System.out.println("Obj1: count is="+obj1.count);
System.out.println("Obj2: count is="+obj2.count);
}
}

Output :
Obj1: count is=2
Obj2: count is=2


As you can see in the above example that both the objects are sharing a same copy of static variable that’s why they displayed the same value of count. 

 static method :

Any method that uses the static keyword is known as a static method. Static Methods can access class variables(static variables) without using object(instance) of the class, however non-static methods and non-static variables can only be accessed using objects.

Example :

class Student
{
int rollno;
String name;
static String college = "IITS";
//static method to change the value of static variable
static void change()
{
college = "ABCD";
}
//constructor to initialize the variable
Student(int r, String n)
{
rollno = r;
name = n;
}
//method to display values
void display() {
System.out.println(rollno + " " + name + " " + college);
}
}
//Test class to create and display the values of object
public class TestStaticMethod
{
public static void main(String args[])
{
Student.change(); //calling change method
//creating objects
Student s1 = new Student(111, "Ram");
Student s2 = new Student(222, "Shyam");
Student s3 = new Student(333, "Raj");
//calling display method
s1.display();
s2.display();
s3.display();
}
}

Output :
111 Ram ABCD
222 Shyam ABCD
333 Raj ABCD


imp points:

  • The static method can not use non static data member or call non-static method directly.
  • this and super cannot be used in static context.

  static block :

Static block is used for initializing the static variables. This block gets executed when the class is loaded in the memory.

Example :

class Test 
{
static int num;
static String str;
static
{
num = 47;
str = "hello";
}
public static void main(String args[])
{
System.out.println("num: " + num);
System.out.println("str: " + str);
}
}

Output :
num: 47
str: hello


static Class :

If a class is nested, it can only be made static.

  • Nested static class doesn’t need reference of Outer class
  • A static class can't access  the Outer class's non-static members.

Example :

class Test
{
private static String str = "hello";

//Static class
static class MyNestedClass
{
//non-static method
public void disp()
{
System.out.println(str);
}

}
public static void main(String args[])
{
/* To create instance of nested class we didn't need the outer
* class instance but for a regular nested class you would need
* to create an instance of outer class first
*/
Test.MyNestedClass obj = new Test.MyNestedClass();
obj.disp();
}
}

Output :
hello

Why is the main method in Java static?

It is because the object is not required to call a static method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.

Tech Smart

Hello, my name is Sam and I am the blog's author. I enjoy blogging since I am passionate about it and I am an enthusiastic learner. Blogging provides me a greater platform to share all of my knowledge and experiences with my readers.

1 Comments

if you have any doubts, Please let me know

Previous Post Next Post