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 Enclosing Instance 2
About Ruby methods
[Ruby] methods, instance methods, etc ...
About pluck and ids methods
About Java class variables class methods
[Ruby] Class methods, instance methods, etc.
About instance variables and attr_ *
[Ruby] Handle instance variables with instance methods
[For our newcomers] About isXXX methods
About =