What is static in java
What is static in java :-
-> In java static is a keyword or it is a modifier.
-> In java there are 8 modifier is available.
- Abstract.
- Final.
- Static.
- Native.
- Volatile.
- Transient.
- Synchronize.
- Strictfp.
-> In case of java these modifier are implemented on a class or on a method or on a variable.
-> By implementing the modifier on a class or on a method or on a variable programmer restrict the class, method and variable.
Type of method in java :-
- Static method / class method.
- Non static method / object method.
- Static method / class method :-
If the class is called by outside class the programmer want to call that function without creating the instance of the class .
2. Non static method / object method :-
If the method is a non static method then it is called outside the class by the help of an object.
Program for static and non static method :-
In this program i am declare a static method (i.e. m1()) and a non static method ( i.e. m2() ) then As m1() is a static method or class method it is called by class name. and As m2() is a non static method or
outside the class it is called by object.
- If you create an object of x class by calling the constructor of x class, then constructor name is same as class name x().
- We call a constructor by new keyword, i.e. new x(); this is a object of x class.
Why main method is static in java :-
- In java as the main method is static it is treated as a class method when the main method is a class method JVM called it by class name without creating the instance of the class.
- If the main method is non static then the program compile successfully but it terminate at runtime as main method is called by JVM.
for more detail follow my Instagram id - https://www.instagram.com/asitmaharana10/
Thank you.
1 Comments
This comment has been removed by the author.
ReplyDeletePost a Comment