java calling a method from another class

后端 未结 2 1219
清酒与你
清酒与你 2020-12-05 05:37

I am working on a problem and I am very stuck because I am just starting to learn java. Any help I can get to understand this would be great. I have to write a program that

相关标签:
2条回答
  • 2020-12-05 06:26

    You're very close. What you need to remember is when you're calling a method from another class you need to tell the compiler where to find that method.

    So, instead of simply calling addWord("someWord"), you will need to initialise an instance of the WordList class (e.g. WordList list = new WordList();), and then call the method using that (i.e. list.addWord("someWord");.

    However, your code at the moment will still throw an error there, because that would be trying to call a non-static method from a static one. So, you could either make addWord() static, or change the methods in the Words class so that they're not static.

    My bad with the above paragraph - however you might want to reconsider ProcessInput() being a static method - does it really need to be?

    0 讨论(0)
  • 2020-12-05 06:27

    You have to initialise the object (create the object itself) in order to be able to call its methods otherwise you would get a NullPointerException.

    WordList words = new WordList();
    
    0 讨论(0)
提交回复
热议问题