Java Static KeyWord Tutorial

Static is a keyword in java and it can be applied to
  • variables
  • methods
  • blocks
  • nested classes
  • interface static methods (introduced in java8)
Static keyword is associated with The Class rather than any Object.

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 below

ClassName.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{
//block of statements
}
Example
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();
Example
package com.mypractice;

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();

}

}
output
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.

2.can we overload static methods?

yes we can overload The static methods.

3.can we override static methods?

No,we cannot override the static Methods.

4.can a class contain multiple static blocks?

yes ,A class can Contain multiple static blocks.order of execution of static blocks is from top to bottom.

5.can we write static public instead of public static in main() method?

yes we can the order of main() method like static public void main().

No comments:

Post a Comment

-->