How to Call a Method in Java

A method is the equivalent of a function in languages like C which helps in code reusing., 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...

11 Steps 3 min read Medium

Step-by-Step Guide

  1. Step 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. Step 2: The word public before the method name means that the method itself can be called from anywhere which includes other classes

    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. , 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()
  3. Step 3: even from different packages (files) as long as you import the class.

    and call the method with "obj.methodExample();". , 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. , For example, if a someMethod() returns an integer, then you can set an integer to what it returns with "int a = someMethod();"

    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. , 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.
  4. Step 4: The second keyword

  5. Step 5: static means that the method belongs to the class and not any instance of the class ( object ).

  6. Step 6: The last word before the method name is void.

  7. Step 7: When calling a method that returns something

  8. Step 8: you can use what it returns.

  9. Step 9: Some methods require a parameter.

  10. Step 10: Methods can also have multiple parameters

  11. Step 11: simply separated by commas.

Detailed Guide

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.

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. , 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();". , 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. , For example, if a someMethod() returns an integer, then you can set an integer to what it returns with "int a = someMethod();"

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. , 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.

About the Author

T

Theresa Sullivan

A passionate writer with expertise in DIY projects topics. Loves sharing practical knowledge.

151 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: