Home > other >  Why is the Jshell not executing Methods cotaining Enum fields?
Why is the Jshell not executing Methods cotaining Enum fields?

Time:07-12

I have a simple Class. Zelle is a simple Enum.

public class Main {

    public void getZelle (){
        System.out.println(Zelle.RED);
    }
    public static void test(){
        System.out.println(Zelle.Empty);
    }

    public static void main(String[] args) {
        System.out.println("HEllo World");

    }

}

If i want to open these Methods with the Jshell i get the following Errors which I do not understand:

jshell> Main.getZelle()
|  Error:
|  non-static method getZelle() cannot be referenced from a static context
|  Main.getZelle()
|  ^-----------^

jshell> Main.test()
|  attempted to use class Main which cannot be instantiated or its methods invoked until variable Zelle is declared

jshell> Main.main(new String[0])
|  attempted to use class Main which cannot be instantiated or its methods invoked until variable Zelle is declared 

But if I run the main() in the IDE it will print my test() Method

public static void main(String[] args) {
        test();
    }

CodePudding user response:

You are accessing the non-static methods in a static way. You can call the non-static methods by creating an object of your class. Your code will look like this:

public class Main {

public void getZelle (){
    System.out.println(Zelle.RED);
}
public static void test(){
    System.out.println(Zelle.Empty);
}

public static void main(String[] args) {
    System.out.println("HEllo World");
    Main main=new Main();  // this is the way to create an o object 
    main.getZelle(); //this is how you call a non-static method using class reference
    main.getTest();


    }

}

CodePudding user response:

You should declare your enum Zelle and create instance of Main class to call non-static methods:

enum Zelle {
  RED, Empty
}


final var main = new Main();
main.getZelle();  // RED
Main.test();  // Empty
Main.main(new String[0]); // HEllo World

CodePudding user response:

You need to import the Enum into the JShell with jshell Nameofenum.java.

CodePudding user response:

Main.getZelle()

You are trying to call method getZelle() as a static method – which it is not – hence the error message because getZelle() is not a static method.

Main.test()

test() is a static method, however it references Zelle (which you claim is an enum but you did not post its definition) and JShell does not know how to find that enum, hence the error message. JShell is trying to compile your class Main but it cannot since Main references Zelle and JShell cannot find the definition of Zelle.

Main.main(new String[0])

Same problem as with Main.test().

Since I couldn't find the definition of Zelle in your question, I guessed what it is and added it to the code that you enter into JShell via its editor. The code is below.

enum Zelle {
    Empty, RED
}

public class Main {

    public void getZelle (){
        System.out.println(Zelle.RED);
    }

    public static void test(){
        System.out.println(Zelle.Empty);
    }

    public static void main(String[] args) {
        System.out.println("HEllo World");
    }
}

Now when I enter Main.main(new String[0]) into JShell, the code executes and I get no error messages. Note, however, that Main.getZelle() will still cause an error because getZelle() is not a static method (as I explained above).

I also recommend that you adopt Java naming conventions. Empty should be EMPTY.

  • Related