static keyword in java

In java, there are various uses of static keyword few of them you can see with example.

static variable :

It is used in java for a single copy in class.

Example :


package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class StaticExample {

static String company = "Infosys Limited";
String name;
String id;
long salary;

public StaticExample(String name, String id, long salary) {
this.name = name;
this.id = id;
this.salary = salary;
}

public static void main(String arg[]) {
StaticExample se = new StaticExample("Pradeep Yadav", "IL981243", 40000);
System.out.println("Employee Name :" + se.name + "\nEmployee Id :" + se.id + "\nEmployee Salary :" + se.salary + "\nCompany Name :" + company);
}
}

Output :

Employee Name :Pradeep Yadav
Employee Id :IL981243
Employee Salary :40000
Company Name :Infosys Limited

—————————————————————–
static block :

When you need to print something before main method called used static block of code.

Example :

package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class StaticExample {

static String company = "Infosys Limited";
String name;
String id;
long salary;

public StaticExample(String name, String id, long salary) {
this.name = name;
this.id = id;
this.salary = salary;
}

public static void main(String arg[]) {
StaticExample se = new StaticExample("Pradeep Yadav", "IL981243", 40000);
System.out.println("Employee Name :" + se.name + "\nEmployee Id :" + se.id + "\nEmployee Salary :" + se.salary + "\nCompany Name :" + company);
}

static {
System.out.println("Excellent work by this employee");
//You can also add some logic here!!
}
}

Output :

Excellent work by this employee
Employee Name :Pradeep Yadav
Employee Id :IL981243
Employee Salary :40000
Company Name :Infosys Limited

——————————————————————

Nested static class :

Through nested static class you can define a class within a class and can access their member.

Example :


package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class StaticExample {

public static void main(String arg[]) {
StaticDemo sd = new StaticDemo();
sd.display();

}

static class StaticDemo {
//Can add some method and variable here!!

void display() {
System.out.println("Nested static class method");
}
}
}

Output :

Nested static class method

—————————————————————————————————-

static method :

When you need to access a method without creating class object used static keyword in method signature.

Example :


package com.pkjavacode.com;

/**
*
* @author pradeep
*/
public class StaticExample {

public static void show() {
System.out.println("static method print");
}

public static void main(String arg[]) {
StaticExample.show();
}
}

Output :

static method print

static keyword in java

static keyword can use with variables, methods, blocks and nested class and can save some memory.

static with variables :
If static used with variable then it will be a single copy in whole class.

example : In this example company name is declared as static variable.


package com.pkjavacode.com;

public class EmployeeDemo {

String name;
int salary;
static String company = "IBM";

public EmployeeDemo(String name, int salary) {
this.name = name;
this.salary = salary;
}

void getEmployeeDetails() {
System.out.print("Employee name :" + name + ", Salary :" + salary
+ ", Company :" + company);
System.out.println();

}

public static void main(String[] args) {
EmployeeDemo e1 = new EmployeeDemo("Pradeep Yadav", 25000);
EmployeeDemo e2 = new EmployeeDemo("Amit Verma", 35000);
e1.getEmployeeDetails();
e2.getEmployeeDetails();
}

}

Output :

Employee name :Pradeep Yadav, Salary :25000, Company :IBM
Employee name :Amit Verma, Salary :35000, Company :IBM

 

 

static with method :
If static keyword used with method then there is no need to create instance to call that method.

example :


package com.pkjavacode.com;

public class EmployeeDemo {
static void getEmployeeDetails() {
System.out.println("static method example");
}

public static void main(String[] args) {
EmployeeDemo.getEmployeeDetails();
}
}

Output :

static method example
static block :
If you want to print something before main() method execution and after class loaded.

example :


package com.pkjavacode.com;

public class EmployeeDemo {

public static void main(String[] args) {

}

static {
System.out.println("This is a static block message");
}
}

Output :

This is a static block message
static with inner class :

If you will used static keyword with inner class then there is no need to create Outer class object, just create inner class object in outer class main() method and can access their methods.

example :


package com.pkjavacode.com;

public class EmployeeDemo {

public static void main(String[] args) {
InnerEmp e = new InnerEmp();
e.display();
}

static class InnerEmp {
void display() {
System.out.println("This is a static inner class message");
}
}
}

Output :

This is a static inner class message