-5

What is the point of creating a class object to call a method in another file when I can just call the file and do the exact same thing?

What I've seen and been taught is that in order to call a method from another file you need to make a class object first, then you call the method using the object

Say you have a method in "FileTwo"

public class FileTwo
{
     public static void method
     {

     }

}

So if I wanted to call "method" in another file, "FileOne" I'd call it like this

public class FileOne
{
     FileTwo file = new FileTwo;
     public static void main(String[] args)
     {
          file.method();
     }
}

But I'm able to do the same thing just by calling the file itself

public class FileOne
{
     public static void main(String[] args)
     {
          FileTwo.method();
     }
}

So what's the point of creating the class object?

  • 5
    The latter will only work if `method` is a static method, in which case the first code is misleading and should be avoided. – Jon Skeet Sep 17 '21 at 18:57
  • Imagine that your class is a dog. You have a method to make a dog eat, and a method to collect dog poop. The first is an instance method, because you can make that specific dog to eat. In this case, you create the specific instance of dog and then ask the dog to eat by calling his instance method. However, collecting poop is something that you can do independently from which dog did it. In this case, you don't need to create a specific dog, just call the static method to collect the poop. Sorry for the poop example but this is how I was explained at first and it worked very well – Matteo NNZ Sep 17 '21 at 19:25
  • Because there is usually more than one thing. – user16632363 Sep 17 '21 at 22:11

2 Answers2

0

the method you're calling is static, means you can call it from both instances or types.

Normally, you use static methods to compute on a facade that doesn't require the object instance to operate.

class math {

    private int x;

    public math(int init){
        x = init;
    }

    public int sum(int a, int b) {
        return a + b;
    }

    public static int sumInternal(int a) {
        return x + a;
    }
}

math.sum(4, 6) doesn't require an instance of math

math.sumInternal(4) requires an instance of math. Notice that it has been declared static

Tia
  • 44
  • 2
0

What is the point of creating a class object to call a method in another file when I can just call the file and do the exact same thing?

Terminology:

  • class instance, or simply object. The phrase "class object" suggests an instance of class java.lang.Class.
  • invoke a method. Everyone will understand "call", but it's not standard in this context
  • methods belong to classes and / or to instances, not to files. This makes a difference for several reasons, not least that one file can contain multiple classes.

To rephrase the question, then:

What is the point of creating an instance of a class to invoke one of its methods when I can just invoke the same method via the class's name, instead?

Answer: when the method you want to invoke is static, there is no special reason to invoke it through an instance, and some style guides specify that in fact, you should not do so. However, most methods of most classes are not static (they are instance methods) and such methods can be invoked only on instances.

As a very rough guide, one creates and uses static methods for operations that impact all objects of a given class or none at all, whereas one uses instance methods for operations of or on one specific instance of that class.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157