0

I want to know, what is the use of the non-static method. My understanding is that static methods can be called directly as well as by the object of the class while nonstatic methods can only be called by an object of the class.

class Ideone
{
    public static void print()
    {
        System.out.println("print");
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        Ideone id = new Ideone();
        id.print();
        print();
    }
}

The above method can be called directly as well as by an object of class. So, when should I have a non static method?

jainilvachhani
  • 708
  • 6
  • 13
  • 1
    https://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods – Hemant Patel May 03 '18 at 05:43
  • 1
    See: https://stackoverflow.com/questions/28612420/what-is-encapsulation-exactly/28614407 – lexicore May 03 '18 at 05:44
  • The question gives the difference between static and instance methods. I want to know when should I use static or non-static methods and if there are any advantages of using non-static methods. – jainilvachhani May 03 '18 at 05:46

1 Answers1

1

In real-world applications objects interacts with other objects and generally have one starting point say main method in some java application.

You CAN NOT invoke other objects nonstatic methods without creating them.

Yati Sawhney
  • 1,372
  • 1
  • 12
  • 19