Java

How to call a Method in Java

In C++, a method (member function) is called, and a static member function is also called. A non-static method is invoked in Java, while a static method is called. Many Java programmers do not bother about the difference between invoking and calling. However, you, this reader, should be a Java programmer with a better image by using the Java vocabulary appropriately.

A class consists basically of fields and methods. In Java, properties are called fields. A method is a group of statements coded together to accomplish a task. Ordinarily, to use a class in Java, the class has to be instantiated into an object. A class is a general-purpose unit from which objects can be instantiated. Instantiating (creating) an object is not really a big deal. It is a very short process in which the fields of the class are given values. Objects of a class differ from one another because they have different values for their same field names. The methods of all the objects and the class are the same.

A static method is a method whose implementation (definition) in a class implementation (definition) begins with the reserved word, static. A static method does not need an object in another for it to execute. On the other hand, a non-static method needs an object in other for it to execute – see details below. In Java, having a non-static method execute invokes the method; having a static method execute is calling the method. A constructor method (simply called constructor) is also called – see below.

This article illustrates how to invoke a non-static method and call a static method.

Class and its Objects

Consider the following class implementation:

    class AClass {
        String str;
AClass (String stg) {
            str = stg;
        }

        void mthd () {
System.out.println(str);
        }
    }

The name of the class is AClass. It has one field, str, which holds a string literally. The class has two methods: the constructor method and a method that returns void. The constructor method does not return anything, and its name is the same as that of the class. The purpose of the constructor method is to initialize some or all of the fields with non-default values. This constructor initializes the field, str, with the value of its parameter (sent argument).

The method of interest in this class is called mthd. It returns void. Its body prints the value of the class’s field. However, this printing can only be done in this case when an object has been instantiated from the class. And so, it is the object that would do the printing, in this case, through the same corresponding method.

An appropriate main class for the above class is as follows:

    public class TheClass {
        public static void main(String[] args) {
AClass obj = new AClass("seen");
obj.mthd();
        }
    }

The main class has the main() method, which should always be there. In this main() method, the first statement instantiates an object, called obj, from the class AClass. It uses the operator, new, and the constructor name, with a given argument, “seen”. “AClass(“seen”)” means calling the constructor. With it, the constructor method is executed to initialize the field, str with “seen”. Other objects instantiated from this same class, AClass, will initialize their own str fields with different string values. The second statement, in the main() method, invokes the method, mthd(), which now prints at the output,

seen

mthd is a non-static method that is invoked (not called). The expression “obj.mthd()” has invoked the method, mthd().

static Method

The following class implementation is similar to the above class, but the same method of interest has been made static:

    class AClass {
        static void mthd (String str) {
System.out.println(str);
        }
    }

This time, there is no constructor method; there is no need for it. The constructor method parameter is now the parameter of the method of interest. The method of interest has been made static by preceding it with the reserved word, static, in the method implementation (definition).

An appropriate main class for the above class is:

public class TheClass {
        public static void main(String[] args) {
AClass.mthd("seen");
        }
    }

The main method here has only one statement. This expression (statement) does not instantiate an object from the class. It just uses the class name, followed by a dot, and then the name of the static method, with its argument, “seen”. The output from the static method is,

seen

The expression, “Aclass.mthd(“seen”)”, calls the static method, mthd(), and does not invoke it. A non-static method is invoked, while a static method is called. A static method does not operate with the instantiated object. It normally operates with the class (name).

static Method in Main Class

The main class is not instantiated. A method in the main class has to be made static. The following program illustrates this:

    public class TheClass {
        static void mthd (String str) {
System.out.println(str);
        }

        public static void main(String[] args) {
mthd("seen");
        }
    }

The method of interest is separate from the main() method. Its implementation has to be preceded with the reserved word, static. The main() method is also static. Both of these static methods are called but not invoked. The method of interest is called with the expression,

mthd("seen");

There is no preceding dot and object or class; because both mthd() and main() are in the same class implementation. The output is:

seen

Conclusion

In C++, a method (member function) is called, and a static member function is also called. In Java, a non-static method is invoked, while a static method is called. Many Java programmers do not bother about the difference between invoke and call. However, this reader should be a Java programmer with a better image by using the Java vocabulary appropriately: a non-static method is invoked, while a static method is called. The illustrations have been made above. Any method in the main class has to be static. This method is called in the main class without preceding it, with a dot and object or class.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.