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.

Steps

  1. 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 is public, static, and void.

  2. 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 replace public. They are protected and private. If a method is protected, then only this class and subclasses (classes that use this as a basis to build off of) can call the method. If a method is private, 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 of public, protected, or private. This is called the default, or package-private. This means that only the classes in the same package can call the method.

  3. 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 keyword static was not there, then the method can be invoked only through an object. For instance, if the class was called ExampleObject and it had a constructor (for making objects), then we could make a new object by typing ExampleObject obj = new ExampleObject();, and call the method with "obj.methodExample();".

  4. The last word before the method name is void. The word void 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 word void with a data type (primitive or reference type) of the object (or primitive type) that you wish to return. Then just add return plus an object of that type somewhere toward the end of the method's code.

  5. 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();"

  6. 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) or someMethod(n) if n is an integer.

  7. Methods can also have multiple parameters, simply separated by commas. If the method someMethod required two parameters, int a and Object 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) where thing is an Object.



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 class Object, there is a non-static method call toString that returns the Object in the form of a String. So, if you wanted to get that String from the Object returned by getObject() 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.

Related Articles