Learn Pain Less

HomeOur TeamContact
Java
What is the use of static keyword in JAVA
Pawneshwer Gupta
Pawneshwer Gupta
March 20, 2015
1 min

Table Of Contents

01
Type of static in JAVA
02
What is static keyword in JAVA ???
03
When to use java static keyword ???
04
Syntax of creating and calling java static fields :
What is the use of static keyword in JAVA

Type of static in JAVA

In JAVA static can be used in three way,

  1. static Variables
  2. static methods
  3. static block

What is static keyword in JAVA ???

static’ is a predefined keyword of JAVA, and it is used to make variables and methods static.

  • static’ means if we make a Variable of class as ’static’ then each and every methods of class will use same copy of variable, they will not create new instance of same variable.
  • By making a variable or methods static, they can be accessed from outside of that class without creating instance of that class.
  • By making a variable or method static they can be used until class not destroyed.
  • By making a method static then that method belong to that class, not an object (instance) of class. (Which means its value remain same)
  • You can not use normal variables in static method, you need to make that variable final to access in static method.
  • You can not class non-static method inside static method.

When to use java static keyword ???

static keyword is used when you want actual value of method which, because you can access method outside class without making instance of a class this means you will directly call to that method, and value of that method will not change. for example :

suppose You have a method which will fetch data from internet and store it in a variable. Now you want to get value of that variable in another class, then you can make that method “static” and you can get its values by this syntax : ClassName.variableName;

Syntax of creating and calling java static fields :

public class Test{
static String msg = "Hello"; // static variable;
static void getMsg(){
// this is static method.
}
}

To call java static variable/method :

public class Message{
String value = Test.msg; // get value from first class, and store in value string;
System.out.println(value); // print value of value string.
}

java static methods and Variables :

To create java static method we simple use ’static’ keyword, and if your methods have some non-static methods or variables then also make then static, because static method can access only static fields.

Syntax of creating static method in non-static class:

public class Main {
public static void main(String[] args) {
Test.getData(); // calling static method from another class directly using class name
System.out.println(Test.sum); // accessing sum variable from another class
}
}
class Test{
static int a = 10;
static int b = 8;
static int sum;
public static void getData(){
sum = a+b;
}
}

So in the given example I have 2 java classes ’Main’ and ’Test’, and inside the ’Test’ class I have some static variables and 1 static method. And I am calling that method and variable in ’Main’ class without creating an instance of ’Test’ class. But if that variables and method should be non-static then its impossible to class them without creating an Instance of ’Test’ class.

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

startstatic keywordjava tutorial

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

Working with Files, Folders java
Working with Files, Folders java
March 25, 2015
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media