Static keyword in Java
The static is a keyword defined in the java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language likewise the static keyword indicates the following :
- The static keyword is applicable to an inner class (a class defined within another class), method or field.
- In java language, a static keyword is used with a class (inner) needed to be instantiated, even this may be referenced by some other class indicating as if it - were a top−level class in the class hierarchy.
- A static keyword can also be used with a field of a class, such a field exists across all the instances of that particular class.
The static keyword is used in java mainly for memory management. We may apply static keyword with variables, methods and blocks. The static keyword belongs to the class than instance of the class. The static can be:
variable (also known as class variable) method (also known as class method) block
static variable
- It is a variable which belongs to the class and not to object(instance)
- Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables
- A single copy to be shared by all instances of the class
- A static variable can be accessed directly by the class name and doesn’t need any object
Syntax :
<class-name>.<variable-name>
Advantage of static variable:
Understanding problem without static variable-
class Employee{ int empId; String empName; String companyName="mindclues"; }
Suppose there are 3000 employees in our company, now all instance data members will get memory each time when object is created.All employees have its unique empId and empName so instance data member is good. Here, company refers to the common property of all objects. If we make it static,this field will get memory only once.
Note:static field is shared by all objects.
Example of static variable:
//Program of static variable
class Employee{ int empId; String empName; static String companyName = "Mindclues"; Employee(int empId, String empName){ this.empId = empId; this.empName= empName; } void display (){ System.out.println(empId+" "+empName+" "+companyName ); } public static void main(String args[]){ Employee employee1 = new Employee(101,"Ram"); Employee employee2 = new Employee(102,"Adam"); employee1.display(); employee2.display(); } }
Output: 101 Ram Mindclues. 102 Adam Mindclues.
static method
- It is a method which belongs to the class and not to the object(instance)
- A static method can access only static data. It can not access non-static data (instance variables)
- A static method can call only other static methods and can not call a non-static method from it.
- A static method can be accessed directly by the class name and doesn’t need any object
- Syntax : <class-name>.<method-name>
- A static method cannot refer to “this” or “super” keywords in anyway
Example of static method:
//Program of changing the common property of all objects(static field). class Employee{ int empId; String empName; static String companyName = "Mindclues"; Employee(int empId, String empName){ this.empId = empId; this.empName= empName; } void display (){ System.out.println(empId+" "+empName+" "+companyName ); } public static void main(String args[]){ Employee employee1 = new Employee(101,"Ram"); Employee employee2 = new Employee(102,"Adam"); employee1.display(); employee2.display(); } }
Output: 101 RKA Mindclues 102 NEXTER Mindclues 103 RAM Mindclues
//Program of accessing non-static data member directly from static method main
class A{
int a=10;//non static
public static void main(String args[]){
System.out.println(a);
}
}
Output:
Compile Time Error
Que)why main method is static?
Ans) because object is not required to call static method if it were non-static method, jvm creates object first then call main() method that will lead the problem of extra memory allocation.
static block:
- Is used to initialize the static data member.
- It is executed before main method at the time of classloading.
- A static block helps to initialize the static data members, just like constructors help to initialize instance members
Example of static block
//Program of static blockclass A{
static{
System.out.println("static block is invoked");
}
public static void main(String args[]){
System.out.println("Hello main");
}
}
Output: static block is invoked Hello main
Can we execute a program without main() method?
-- Yes, one of the way is static block but in previous version of JDK not in JDK 1.7.
//Program of executing a program without main() method
class A{ static{ System.out.println("static block is invoked"); System.exit(0); } }
post a comment