What is Static Keyword in Java?Static means a field or an method that belongs to the class, rather than to an instance of the class.In simple terms, it means that we can call a static method/field even without creating the object of a class to which they belongs!.We can have a static method or a static variable.
We have used this concept in our public static void main(String args), while using this we never created the object of main method but instead Java virtual machine can call the main method without creating a new application object.
Note:- we cannot use Static with class unlike final keyword.
|Static class is not possible!!|
How to access static method/VariableSyntax:-
We would be accessing the static method of class(StartURL) using StartURL.testURL();
Note:- We can still access the static method or variable by using the instance object of the class, but this is not a good coding practice.In above case we could have done it using :-
Still Eclipse or other Java IDE's would generate warning message :-
1.Static method is associated with a class while non static method is associated with an object.
2.We can use Static + final to create constant value that's attached to a class.
3.When we create different object of an class, each class contains its distinct copy of instance variable and each is stored in different memory location but incase of static variables, it is shared across the class , which occupies an fixed location in memory.So by using static keyword we are making our program memory efficient.
4.Non static (methods, variables) can not be accessible inside static methods,meaning we can only access static stuff Inside static methods.
Interface in Java