Static is a keyword in java and it can be applied to
Example
Example
because method should invoked at the time of class loading so with out creating instance of class we can call the main() method.
- variables
- methods
- blocks
- nested classes
- interface static methods (introduced in java8)
Static Variable
The variable which can be declared With static keyword is known as static variable. static varibles will keep common value to all objects.Every Object Of The class shares The static variable.Example
package com.mypractice;
public class Employee {
private int employeeId;
private String Name;
private static String department = "AAAAA";
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public static void main(String[] args) {
Employee employee1 = new Employee();
employee1.setEmployeeId(1);
employee1.setName("A");
System.out.println(employee1.getEmployeeId()+"---"+employee1.getName()+"---"+department);
System.out.println("--------------------------------");
Employee employee2 = new Employee();
employee2.setEmployeeId(2);
employee2.setName("B");
System.out.println(employee2.getEmployeeId()+"---"+employee2.getName()+"---"+department);
System.out.println("--------------------------------");
Employee employee3 = new Employee();
employee3.setEmployeeId(3);
employee3.setName("C");
System.out.println(employee3.getEmployeeId()+"---"+employee3.getName()+"---"+department);
}
}
Output
1---A---AAAAA
--------------------------------
2---B---AAAAA
--------------------------------
3---C---AAAAA
Static Method
The Methods which are declared with static keyword is known as static methods.static methods should be invoked with the class name, without creating an Object of class like belowClassName.methodName(args);
We can use static methods is to access the static fields and we can change the value of static fields.Example
package com.mypractice;
public class Employee {
private static String department = "AAAAA";
public static void staticMethod() {
System.out.println("static variable value is " + department);
System.out.println("----------------");
department = "BBBB";
System.out.println("static variable value is " + department);
}
public static void main(String[] args) {
Employee.staticMethod();
}
}
Output
static variable value is AAAAA
----------------
static variable value is BBBB
Static Block
A static Block is a normal Block of code enclosed in braces{},static block can be executed at the time of class loading.static block is used to intialize the static members.structure of static block
static{Example
//block of statements
}
package com.mypractice;
public class Employee {
static {
System.out.println("static block");
}
public static void main(String[] args) {
}
}
Output
static block
Static Nested classes
If you create static class inside class is known as static nested class.Static nested classes are accessed using the enclosing class name.OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
Examplepackage com.mypractice;output
public class OuterClass {
private static String name = "aaaaa";
static class InnerClass {
public void innerMethod() {
System.out.println("nested class");
System.out.println(name);
}
}
public static void main(String[] args) {
OuterClass.InnerClass nestedObject = new OuterClass.InnerClass();
nestedObject.innerMethod();
}
}
nested class
aaaaa
Some Interview Questions on Static keyword in java?
1.why main() method is declared as static?because method should invoked at the time of class loading so with out creating instance of class we can call the main() method.