Call a Method in Java
When beginning programming in Java, there are many new concepts to learn. There are Name a Class in Java, methods, Handle Exceptions in Java, constructors, variables, and more, and it can become overwhelming. So, it is best to learn piece by piece. In this how-to article, you will learn how to call a method in java.
Contents
Steps
- A method is the equivalent of a function in languages like C which helps in code reusing. A set of statements make a method, and this method can be invoked through other statement. When invoked (called) , all the statements that are a part of the method would be executed. For instance, look at this method: "
public static void methodExample() {}". It currently has no code in it, but there are three keywords before the method name. There ispublic,static, andvoid. - The word
publicbefore the method name means that the method itself can be called from anywhere which includes other classes, even from different packages (files) as long as you import the class. There are three other words that can replacepublic. They areprotectedandprivate. If a method isprotected, then only this class and subclasses (classes that use this as a basis to build off of) can call the method. If a method isprivate, then the method can only be called inside the class. The last keyword is really not even a word. This is if you had nothing in the place ofpublic,protected, orprivate. This is called the default, or package-private. This means that only the classes in the same package can call the method. - The second keyword,
staticmeans that the method belongs to the class and not any instance of the class ( object ). Static methods must be called using the class name: "ExampleClass.methodExample()". However, if the keywordstaticwas not there, then the method can be invoked only through an object. For instance, if the class was calledExampleObjectand it had a constructor (for making objects), then we could make a new object by typingExampleObject obj = new ExampleObject();, and call the method with "obj.methodExample();". - The last word before the method name is
void. The wordvoidmeans that the method doesn't return anything (it does not return anything when you run the method). If you do want a method to return something, then simply replace the wordvoidwith a data type (primitive or reference type) of the object (or primitive type) that you wish to return. Then just addreturnplus an object of that type somewhere toward the end of the method's code. - When calling a method that returns something, you can use what it returns. For example, if a
someMethod()returns an integer, then you can set an integer to what it returns with "int a = someMethod();" - Some methods require a parameter. A method that requires a parameter of an integer would look like
someMethod(int a)When using a method like this, you would write the method name, and then an integer in the parentheses:someMethod(5)orsomeMethod(n)ifnis an integer. - Methods can also have multiple parameters, simply separated by commas. If the method
someMethodrequired two parameters,int aandObject obj, it would look like "someMethod(int a, Object obj)". To use this new method, it would be called by the method name followed by an integer and an Object in parentheses:someMethod(4, thing)wherethingis anObject.
Tips
- When calling a method that returns something, you can call another method based off of what that method returns. Let's say we have a method called
getObject()that returns an object. Well, in the classObject, there is a non-static method calltoStringthat returns theObjectin the form of aString. So, if you wanted to get thatStringfrom theObjectreturned bygetObject()in one line, you just would write "String str = getObject().toString();".
Warnings
- Be careful about abstract classes and methods. If a method is abstract, it cannot be used until it is implemented by another class. This is because an abstract method doesn't have any code in it in the first place. Abstract classes are used as a sort of framework.