How to Call a Method in Java (with Pictures)

Table of contents:

How to Call a Method in Java (with Pictures)
How to Call a Method in Java (with Pictures)
Anonim

When you take your first steps in Java programming, you immediately realize that there are many new concepts to learn. If you want to learn programming in Java, you have to run into things like classes, methods, exceptions, constructors, variables, and many other objects, so it's very easy to get overwhelmed and frustrated. To avoid this, it is best to proceed step by step, one step at a time. This article explains how to use methods in Java.

Steps

972649 1
972649 1

Step 1. Understand the meaning of 'method'

In Java, a method is represented by a series of instructions that give life to a function. After declaring a method, it will be possible to call it from elsewhere in the program to execute the code that composes it. This is a very useful way to be able to effectively reuse the code already created, thus avoiding repetitions and redundancies. Below is the sample code of a very simple method.

    public static void methodName () {System.out.println ("This is a method"); }

972649 2
972649 2

Step 2. Declare the class that will have to access the method

When declaring a Java method, you must also declare which classes will have access to the method code. In the example code, the method was declared public by using the "Public" parameter. You can manage access to a method using three access modifiers:

  • Public - using the "public" parameter in the method declaration, it indicates that all classes will be able to call this method;
  • Protected - with the "protected" parameter, it is indicated that the method can be called and used only by the class that contains it and by any subclasses present;
  • Private - if a method is declared of type

    private

  • , it means that the method can only be called within the class in which it was declared. In this case, it is referred to as the default method or private package. This means that only classes defined within the same package will have access to this method.
972649 3
972649 3

Step 3. Declare the class to which the method belongs

Continuing with the example method, the second parameter of the declaration is "static", indicating that the method belongs to the class and not to any instance of that class. "Static" methods must be invoked using the name of the class they belong to: "ClassExample.methodExample ()".

If the "static" parameter is omitted from the method declaration, it means that the method can only be invoked using a Java object. For example, if the class to which the method in question belongs is called "ClasseExample" and has a constructor (a special method used to create the object of type "ClasseExample"), you can create a new object for the class using the following code "ClasseExample obj = new ClasseExample ();". At this point, you can call the method using the following command: "obj.metodoExample ();"

972649 4
972649 4

Step 4. Declare the value that the method should return

This part of a method declaration is used to indicate the type of object that will be returned by the method. In the previous example, the "void" parameter specifies that the method will not return any value.

  • If you need the method to return an object, simply replace the "void" parameter with the data type (primitive or a reference to a data type) to which the object that will be returned belongs. Primitive data types include int integers, float, double decimal values, and many other standard data types. At this point, add the "return" command followed by the object that must be returned before the end of the code that makes up the method.
  • When calling a method that returns an object, you can use that object to perform other processing. For example, assume you have a method called "methodTest ()" which returns an integer value (ie a number) that you can use to initialize a variable of type "int" using the following code: "int a = methodTest ();"
972649 5
972649 5

Step 5. Declare the method name

Once you've indicated the classes that can have access to the method, the class it belongs to, and what it returns, you'll need to name the method so you can call it wherever you want. To perform this step, simply type the name of the method followed by an open and a closed perentesis. In the previous examples, there are the "testmethod ()" and "methodName ()" methods. After declaring a method, you can add all the instructions that make it up by enclosing them in braces "{}".

972649 6
972649 6

Step 6. Call a method

To be able to call a method, simply type the corresponding name, followed by an opening and a closing parenthesis, at the point in the program where you want to execute the method. Remember to call the method only within a class that can have access to that method. The following example code declares a method which is then called within its class:.

    public class ClassName {public static void MethodName () {System.out.println ("This is a method"); } public static void main (String args) {methodName (); }}

972649 7
972649 7

Step 7. Add the input parameters of the method (if necessary)

Some methods require you to use input parameters to be called correctly, for example an integer value (a number) or a reference to an object (for example, the name of that object). If the method you want to use needs one or more input parameters, you just need to put them in parentheses right after the method name. A method that requires an integer value as a parameter will have the following syntax "methodName (int a)" or very similar code. A method that accepts an object reference as a parameter will have the following syntax "methodName (Object obj)" or similar code.

972649 8
972649 8

Step 8. Invoke a method with an input parameter

In this case, simply insert the name of the parameter in parentheses, immediately after the name of the method to be called. For example "methodName (5)" or "methodName (n)", provided that the variable "n" is of type "integer". If the method needs a reference to an object, you simply need to insert the name of that object in round brackets immediately after the method name. For example "methodName (4, objectName)".

972649 9
972649 9

Step 9. Use multiple parameters in method call

Java methods can accept more than one input parameter. In this case, you will need to separate each parameter with a comma. In the example code that follows, a method is created that must add two integers together and return the value of the sum. When the method is to be called, the two numbers to be added must be specified as input parameters. After running this simple Java program, the result will be the string "The sum of A and B is 50". Here is the Java code:

    public class myClass {public static void sum (int a, int b) {int c = a + b; System.out.println ("The sum of A and B is" + c); } public static void main (String args) {sum (20, 30); }}

Advice

  • When calling a method that must return an object or value, you can use that value to invoke another method that has the same data type returned by the first method as its input parameter. For example, assume you have a method called

    getObject ()

    which returns an object as a result. The class

    Object

    contains the method

    toString

    defined as non-static, which returns the object

    Object

    of type

    String

    . After this premise, in case you need to get from the method

    getObject ()

    the item

    Object

    of type

    String

    implementing all the procedure in a single line of code you simply have to write the following:"

    String str = getObject (). ToString ();

  • ".

Recommended: