Learn Static Keyword In Java

Sharing is healthy !!

Today we will learn static keyword in java.

In this post we will see what does the static keyword do in a class.

In my previous posts you may have seen the usage of keyword “static” with some member method or member variables of a class and you maybe curious what is the meaning of this….

Learn Static Keyword In Java: static method

The meaning behind the static keyword is whenever you declare your member as static the static member belongs to the class instead of some specific instance of a class. Static method can access static data member and can change the value of it.

Let me give you an example,

public class Welcome 
{
public static String Greetings(String message)
{
return message;
}
 
public String GreetWithSmile(String message)
{
return message;
}
}
public class Static_Example 
{
public static void main(String[] args) 
{
Welcome wc = new Welcome();
Welcome.Greetings("Welcome to Flower Brackets!!");
wc.GreetWithSmile("Hi!! Flower Brackets.");
}
}

I have a class called Welcome and which has two methods.

One is a static method and other is a nonstatic method. Static method means which contains “static” keyword in it. This method is not doing much it’s just taking a string as an argument and it’s returning string.

Also Read –¬†Switch Case Example In Java

We have another method. It has different name which is “GreetWithSmile” and this does not have any static keyword in it.

The static member only belongs to the class not to the instance. So it will not belong to the instance of a class.

So if you do something like this,

wc.Greetings("Welcome to Flower Brackets!!");

Greetings which is a static method and we want to pass some message “Welcome to Flower Brackets!!” for example.

It will work and it will work fine but it will give some warning like this,

Learn Static Keyword In Java

And the warning is “The static method Greetings from the type Welcome should be accessed in a static way”. So you cannot access “Greetings” method which is declared as static method.

You can access¬†“Greetings” method directly from a class like this,

Welcome.Greetings("Welcome to Flower Brackets!!");

But when you try to access non static method ,

Learn Static Keyword In Java

“GreetWithSmile” method is not available in Welcome class or cannot be accessed directly with the class name. “Greetings” method is accessed from the class and “GreetWithSmile” method which is not a static method cannot be accessed directly by Welcome class.

You can see (in the above image) there is no “GreetWithSmile” method. But nonstatic method is available through the instance of a class.

Basically the nonstatic methods are available or can be accessed using the instance of a class and the static method should be accessed using the class itself not the instance.

Below is the correct way of using non-static method,

wc.GreetWithSmile("Hi!! Flower Brackets.");

Learn Static Keyword In Java: static variable

You can even declare static variable for example,

public class Welcome 
{
public static int age;
public static String Greetings(String message)
{
return message; 
}
public String GreetWithSmile(String message)
{
return message; 
}
}

We have declared a static variable “age” in the above example.

And we access static variable in main method like this,

public class Static_Example
{
public static void main(String[] args)
{
Welcome wc = new Welcome();
Welcome.Greetings("Welcome to Flower Brackets!!");
wc.GreetWithSmile("Hi!! Flower Brackets.");
Welcome.age = 50;
}
}

conclusion:

In this way whenever you want to declare a variable or method static just access them using a class and whenever you are using non static method or variable access them with the instance of a class. I hope you understood this concept.

Sharing is healthy !!