Archer
Archer

Reputation: 67

How to tell compiler what method I am going to use if have two methods with the same names in Java?

How to use a method from java.lang.Math class if I imported static methods. I know I can just write Math.abs(-12), but are there other ways to do this?

import static java.lang.Math.*;

public class Lesson1 {

  private static int abs(int x) {
    System.out.println("My abs method");
    return x;
  }

  public static void main(String[] args) {
    System.out.println(abs(-12));
  }

}

Upvotes: 1

Views: 63

Answers (1)

dariosicily
dariosicily

Reputation: 4517

If you want to use your custom method abs , call it with Lesson1.abs(-12). If you want to use java.lang.Math.abs call it with Math.abs(-12) or directly abs(-12) cause the static import.

Upvotes: 2

Related Questions