About calling instance methods

A reminder about the format of instance method calls.

About the method call format

--[Method name (argument)] for methods defined in the same instance --[Variable.method name (argument)] for the method defined in the instance --For static methods, [class name.method name (argument)]

Methods defined in the same instance

Test.java


public class Test {
  public void SayHello() {
    System.out.println("Hello World");
  }
  public void Display() {
    SayHello();
  }
}

Methods defined in different instances

Create a new instance and use the reference that contains the variable (hello.SayHello ();).

Hello.java


public class Hello{
  public void SayHello() {
    System.out.println("Hello World");
  }
}

Test.java


public class Test {
  public static void main(final String[] args) {
    Hello hello = new Hello();
    hello.SayHello();
  }
}

static method

Use class name references (Hello.SayHello ();).

Hello.java


public class Hello{
  public static void SayHello() {
    System.out.println("Hello World");
  }
}

Test.java


public class Test {
  public static void main(final String[] args) {
    Hello.SayHello();
  }
}

bonus

The ones without parentheses are the access to the fields of the instance.

References

Thorough capture Java SE11 Silver problem collection

Recommended Posts

About calling instance methods
About Ruby instance methods
About singular methods
About HttpServlet () methods
About Enclosing Instance 2
About Ruby methods
Find out about instance methods and self
[Ruby] About instance generation
[Ruby] methods, instance methods, etc ...
About an instance of java
About validation methods in JUnit
About naming Rails model methods
About pluck and ids methods
About Java class variables class methods
[Ruby] Class methods, instance methods, etc.
About instance variables and attr_ *
About methods often used in devise
About Java static and non-static methods
[Ruby] Handle instance variables with instance methods
[For our newcomers] About isXXX methods
About =
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.