TrippelS
TrippelS

Reputation: 58

Initializing a class retrieved by a function Java for android

My first question on stackoverflow:

Ok so I am writing a java game for android and I am having trouble with initializing certain classes retrieved from a function/method. Here is my code:

static int unUsedCharacters = 10;
static Character char1;
static Character char2;
static Character char3;
static Character char4;
static Character char5;
static Character char6;
static Character char7;
static Character char8;
static Character char9;
static Character char10;

Character getFreeCharacter() {
        if (char1 == new Character()) {
            return char1;
        } else if (char2 == new Character()) {
            return char2;
        // and so on... until 10
        } else {
            return char10;
        }
    }

    public void createCharacter(String x) {
        if (unUsedCharacters > 0) {
            unUsedCharacters -= 1;
            getFreeCharacter() = new Warrior(); 
            //the warrior class extends the character class
        } else {
            /* no more characters */
        }
    }

So The problem is where I try to do:

    getFreeCharacter() = new Warrior();

It says:

Variable expected.

Any suggestions? (It is probarly super easy, but this is all new to me)

Thank you for reading/responding

Upvotes: 1

Views: 177

Answers (1)

John Bollinger
John Bollinger

Reputation: 181008

An expression of this form...

 (char1 == new Character())

... will always evaluate to false (or throw an exception). The new operator never produces a null result, and it never produces a reference to an object that existed before, but those alternatives cover all the possible values of char1 at any time. More likely you want something like this:

if (char1 == null) {
    char1 = new Character();
    return char1;
}

In truth, though, I would never hold the ten characters in ten independent variables. I'd probably use a List of Characters, or at worst an array.

Having gotten past that, you have a different problem. The Character you return is already an object assigned to the intended slot. You can set its properties (to the extent that its class allows you to do so), but you cannot replace it with a different object by assigning to it. Indeed, you cannot assign directly to the method result at all, but even if you stored it in a variable, assigning a different object to that variable would not have the effect you want.

(Note, by the way, that I am distinguishing between assigning to members of the method return value, which you can do, and assigning to the value itself, which you cannot do.)

I think you would be best off getting rid of getFreeCharacter() altogether. If you structure your class better then you won't need it. Instead of declaring char1 ... char10 like so:

// Yuck, don't do this
Character char1;
Character char2;
// ...
Character char1;

... do as I suggested earlier and use a List:

import java.util.ArrayList;
import java.util.List;

public MyGame {

    List<Character> characters = new ArrayList<>();

    // ...

}

Then, your createCharacter() method can have this form:

public void createCharacter(String x) {
    if (characters.size() < 10) {
        characters.add(new Warrior()); 
    } else {
        /* no more characters */
    }
}

Elsewhere, you can retrieve elements of the character list via its get() method, e.g. characters.get(0). The List instance takes care of a whole lot of details that you will otherwise have to write your own code for.

Upvotes: 1

Related Questions