Edward Talanian Edward Talanian - 10 months ago 54
Java Question

Calling a static method play() Java

I'm falling a little behind in my Java class (pun not intended) and I'm slightly confused about secondary methods (if that's what they're called). It's an online class so there is minimal help from the teacher. Anyways, we're creating a Rock Paper Scissors game in an RPS Class (all code in one file). To quote the assignment:

main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program ends. play() has no parameters, and no return value.

I'm not asking for anyone to do this for me, but I don't understand how to initiate this 'static method play() of the RPS class'.

Thanks in advance!

Answer Source

Since everything is in a single class, it should separate code into methods for organization.

You will need the main method, "public static void main(...)", however if you want to make any extra methods that are not contained within an another (instance) class then those methods need to be declared as static as well.

So it would look something like this...

public class RPS{

    public static void main(String[] args){

    private static void play(){...}