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
public
before 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 areprotected
andprivate
. 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,
static
means 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 keywordstatic
was not there, then the method can be invoked only through an object. For instance, if the class was calledExampleObject
and 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 wordvoid
means 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 wordvoid
with a data type (primitive or reference type) of the object (or primitive type) that you wish to return. Then just addreturn
plus 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)
ifn
is an integer. - Methods can also have multiple parameters, simply separated by commas. If the method
someMethod
required two parameters,int a
andObject 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)
wherething
is 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 calltoString
that returns theObject
in the form of aString
. So, if you wanted to get thatString
from theObject
returned 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.