Error Cannot make a Static Reference to the non-static method from the type

Asked

Viewed 164 times

0

I’m trying to do this exercise and it returns this error:

Cannot make a Static Reference to the non-static method triple(int) from the type Minhasfuncoes

public class MinhasFuncoes {

    public int triplo(int numero){
        return numero * 3;
    }
    public static void main(String[]args) {
        System.out.println(triplo(3));
    }
}

1 answer

0


You have to declare the function access method as Static.

public static int triplo(int numero){
    return numero * 3;
}

If you do not want to use as a static method you must create an instance of an object in order to use the method tripo, that your object API MinhasFunções exposes. Here is an example:

public class MinhasFuncoes {

public int triplo(int numero){
    return numero * 3;
}
public static void main(String[]args) {
    MinhasFuncoes mf = new MinhasFuncoes();
    System.out.println(mf.triplo(2));     
}
  • Thanks!! Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.