Static Method in Java

Previous
Next

Static user method:

  • Every program is a collection of methods.
  • Main() method is the starting point of application.
  • JVM invokes the main() method automatically.
  • We can define user methods depends on application requirement.
  • Static methods must be defined with static keyword.
public class Pro
{
     public static void main(String []args)
     {
        System.out.println("main...");
     }
     
     static void test()
     {
         System.out.println("test...");
     }
}
  • In the above application, only main() method invokes by default.
  • Test() method must be called explicitly by the programmer.
  • We access static members(variable, method) using class name.
public class Pro
{
     public static void main(String []args)
     {
        System.out.println("main starts...");
        Pro.test();
        System.out.println("main ends...");
     }
     
     static void test()
     {
         System.out.println("test...");
     }
}
  • We can define more than one method in a single class.
  • Class members can be defined in any order.
  • Methods execute only when we call explicitly.
  • Once the method has been defined, we can call as many times depends on requirement.
public class Pro
{
    static void m2()
     {
         System.out.println("In m2");
     }
     
     public static void main(String []args)
     {
        System.out.println("main");
        Pro.m1();
        Pro.m1();
        Pro.m1();
        Pro.m2();
        Pro.m1();
     }
     
     static void m1()
     {
         System.out.println("In m1");
     }
}
  • Static block executes before main() method.
  • User methods execute only when we invoke
public class Pro
{
    static void fun()
     {
         System.out.println(3);
     }
     
     public static void main(String []args)
     {
        System.out.println(5);
        Pro.fun();
        System.out.println(1);
     }
     
     static
     {
         System.out.println(7);
         Pro.fun();
         System.out.println(4);
     }
}
  • Method takes input called arguments.
  • Arguments need to place in parenthesis.
  • Arguments are working like local variable.
  • We access local variables directly.
public class Pro
{
    static int a=10;
    public static void main(String []args)
    {
        Pro.test(20);
        System.out.println(a + Pro.a);
    }
     
    static void test(int a)
    {
        System.out.println(a + Pro.a);
    }
}
public class Pro
{
    static int a;
    static
    {
        Pro.test(10);
    }
    public static void main(String []args)
    {
       System.out.println(Pro.a);
    }
     
    static void test(int a)
    {
       System.out.println(Pro.a);
       Pro.a = a;
    }
}

Analyze the code:

public class Pro
{
    public static void main(String []args)
    {
       Pro.add(10,20);
       Pro.add(34,67);
       Pro.add(-23, 45);
    }
     
    static void add(int a, int b)
    {
       System.out.println("Sum is : " + (a+b));
    }
}
Previous
Next

Add Comment

Courses Enquiry Form