Sozziko
Sozziko

Reputation: 129

Calling Static Void Methods

I'm still learning to use methods, but I hit another stump in the road. I'm trying to call a static void method in another static void method. So it roughly looks like this:

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

//Method for total amount of people on earth
//Must call plusPeople in order to add to total amount of people
 public static void thePeople (int[][] earth) {
How to call plusPeople?
}

//Method adds people to earth
//newPerson parameter value predefined in another class.
public static void plusPeople (int[][] earth, int newPerson) {
earth = [newPerson][newPerson]
}

I've tried a few different things which hasn't really worked.

int n = plusPeople(earth, newPerson); 
//Though I learned newPerson isn't recognized 
because it is in a different method.

int n = plusPeople(earth); \
//I don't really understand what the error is saying,
but I'm guessing it has to do with the comparison of these things..[]

int n = plusPeople;
//It doesn't recognize plusPeople as a method at all.

I feel very stupid for not being able to even call a method, but I've literally been stuck on this issue for about two hours now.

Upvotes: 1

Views: 12101

Answers (2)

JB Nizet
JB Nizet

Reputation: 692043

You need to provide two arguments. The first one needs to be of type int[][] (and earth qualifies), and the second one needs to be an int. So, for example:

plusPeople(earth, 27);

Of course, that's only a technical answer. What you should really pass as argument depends on what the method does with its arguments (which should be specified in its javadoc), what the arguments mean (which should be specified in its javadoc), and what you want the method to do for you (which you should know).

Also, note that since the method is declared as void plusPeople(...), it doesn't return anything. So doing int n = plusPeople(earth, newPerson); doesn't make any sense.

Upvotes: 2

Jan Gorzny
Jan Gorzny

Reputation: 1772

If it's void, you can't assign it to anything.

Just call using

int n = 5;
plusPeople(earth, n);

The first error you're getting is because the newPerson isn't defined locally (you're right, it's in a different method) The second error you're getting is because the return type "void" was not an "int". The third error you're getting is because it has no parenthesis, and probably thinks there should be a variable named plusPeople.

Upvotes: 3

Related Questions