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.

  1. Abstract.
  2. Final.
  3. Static.
  4. Native.
  5. Volatile.
  6. Transient.
  7. Synchronize.
  8. 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.

What is static in java


Type of method in java :-

-> Java popularly use 2 type of method.
  1. Static method / class method.
  2. Non static  method / object method. 

  1. 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  :-

class x
{
 static void m1() //static method.
{
 System.out.println("this is a class method");
}
void m2()  //non static method.
{
System.out.println("this is a object method");
}
}
public class v  // Here v class is called as driver class of main class because it contain the main method. 
{
public static void main (String args[])
{
x.m1();       // As m1() is a static method or class method it is called by class name.
new x().m2();   // As m2() is a non static method or outside the class it is called by object.
}
}

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.

What is static in java

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.