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:
Syntax : <class-name>.<variable-name>
Advantage of static variable:
Understanding problem without static variable-
class Employee{ int empId; String empName; String compName="DAV JavaServices Ltd."; }
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 compName = "DAV JavaServices Ltd."; Employee(int empId, String empName){ this.empId = empId; this.empName= empName; } void display (){ System.out.println(empId+" "+empName+" "+compName ); } public static void main(String args[]){ Employee employee1 = new Employee(111,"Dinesh"); Employee employee2 = new Employee(222,"Sweety"); employee1.display(); employee2.display(); } }
Example of static method:
//Program of changing the common property of all objects(static field). class Employee{ int empId; String empName; static String compName = "DAV JavaServices Ltd."; static void change(){ compName = "DOJ Ltd"; } Student(int empId, String empName){ this.empId = empId; this.empName = empName; } void display (){ System.out.println(rollno+" "+name+" "+college); } public static void main(String args[]){ Employee.change(); Employee employee1 = new Employee(111,"Dinesh"); Employee employee2 = new Employee(222,"Sweety"); Employee employee3 = new Employee(333,"Anamika"); employee1.display(); employee2.display(); employee3.display(); } }
//Program of accessing non-static data member directly from static method main class A{ int a=40;//non static public static void main(String args[]){ System.out.println(a); } }
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:
Example of static block
/Program of static block class A{ static{ System.out.println("static block is invoked"); } public static void main(String args[]){ System.out.println("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); } }
static block is invoked (if not JDK7)
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…