Download ArticleDownload Article

When beginning programming in Java, there are many new concepts to learn. There are classes, methods, exceptions, constructors, variables, and more, and it can become overwhelming. So, it is best to learn piece by piece. This How.com.vn teaches you how to call a method in Java.

  1. How.com.vn English: Step 1 Understand what a method is.
    In Java, a method is a series of statements that create a function. Once a method is declared, it can be called at different parts of the code to execute the function. This is an useful way to reuse the same code over and over again. The following is an example of a simple method.
        public static void methodName() {  System.out.println("This is a method");}
  2. How.com.vn English: Step 2 Declare the class access for the method.
    When declaring a method in Java, you need to declare what classes can access the method. In the example above, the access is declared as "Public". There are three access modifiers you can declare a method:
    • Public: By placing the access modifier "public" before the method name allows the method to be called from anywhere.
    • Protected: The "protected" access modifier, only allows the method to be called within it's class and subclasses.
    • Private: If a method is declared private, then the method can only be called inside the class. This is called the default, or package-private. This means that only the classes in the same package can call the method.
    Advertisement
  3. How.com.vn English: Step 3 Declare the class the method belongs to.
    In the example above, 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()".
    • If the keyword "static" was not used, 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 using the following: "obj.methodExample();".
  4. How.com.vn English: Step 4 Declare the return value.
    The return value declares the name of the value the method returns. In the example above the word "void" means that the method doesn't return anything.
    • 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. Primitive types include int, float, double, and more. Then just add "return" 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 method called "someMethod()" returns an integer (a number), then you can set an integer to what it returns using the code: "int a = someMethod();"
  5. How.com.vn English: Step 5 Declare the method name.
    After you've declared the classes that can access the method, the class it belongs to and the return value, you need to give the method a name so that it can be called. To give the method a name, simply type the method name followed by an open and closed parenthesis. The examples above include, "someMethod()" and "methodName()". You would then input all the method statements inside opened and closed curly brackets "{}"
  6. How.com.vn English: Step 6 Call the method.
    To call a method, you just need to type the method name followed by open and closed parentheses on the line you want to execute the method. Make sure you only call a method within a class that has access to it. The following is an example of a method that is declared and then called within the class:[1].
      public class className {  public static void methodName(){  System.out.println("This is a method");} public static void main(String[] args) {    methodName();  }}
  7. How.com.vn English: Step 7 Add a parameter to a method (if needed).
    Some methods require a parameter such as an integer (a number) or a reference type (such as the name of an object). If a method requires a parameter, you just simply type the parameter in between the open and closed parenthesis after the method name. A method that requires an integer parameter of an integer would look like "someMethod(int a)" or similar. A method that has uses a reference type would look like "someMethod(Object obj)" or similar.
  8. How.com.vn English: Step 8 Call a method with a parameter.
    When calling a method that requires a parameter, you would just simply add the parameter in the parethesis after the method name. For example:"someMethod(5)" or "someMethod(n)" if "n" is an integer. If the method requires a reference object, simply enter the name of the object in the open and closed parenthesis. For example, "someMethod(4, thing)".
  9. How.com.vn English: Step 9 Add multiple parameters to a method.
    Methods can also have multiple parameters, simply separated by commas. In the following example, a method is created to add two integers together and return the sum as the return method. When the method is called, the two integers are given as parameters will be added together. When the program is run, you will receive an output that says "The sum of A and B is 50".:
      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); }}
  10. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How can I create objects?
    How.com.vn English: Pingu
    Pingu
    Top Answerer
    You create objects by invoking the constructor of their class (it has the same name as the class). Java already has some classes; for example, Integer, but you can also define your own class. For example, if you defined a class Orange, you could create an object of that class like this: "Orange o = new Orange();".
  • Question
    How can we call the second method while using the main one?
    How.com.vn English: Community Answer
    Community Answer
    The main is what runs first, so make sure you call the second method from inside the main { }, and it will execute by itself.
  • Question
    What is a method call in Java?
    How.com.vn English: Community Answer
    Community Answer
    A method is a set of code that is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value. Each method has its own name.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Video

      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();".
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

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

      About This Article

      How.com.vn English: Travis Boylls
      Written by:
      How.com.vn Technology Writer
      This article was co-authored by How.com.vn staff writer, Travis Boylls. Travis Boylls is a Technology Writer and Editor for How.com.vn. Travis has experience writing technology-related articles, providing software customer service, and in graphic design. He specializes in Windows, macOS, Android, iOS, and Linux platforms. He studied graphic design at Pikes Peak Community College. This article has been viewed 718,636 times.
      How helpful is this?
      Co-authors: 19
      Updated: November 19, 2022
      Views: 718,636
      Categories: Java
      Article SummaryX

      1. Declare the access of a method (i.e. public, protected, private).
      2. Declare the class the method is in (i.e. static).
      3. Declare the return value of the method (i.e. void, int).
      4. Declare the name of the method followed by open and closed parenthesis.
      5. Add parameters (arguments) in the parenthesis separated commas.
      6. Call the method by typing the name of the method followed by parenthesis.

      Did this summary help you?

      Thanks to all authors for creating a page that has been read 718,636 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement