[Java] (beginner) Is there a way to convert void (an object's state) to boolean?

if (ship1.contains(point)){ //points is the mouse location
		ship1.show();
		points= points +500;}
	}

Right now the code will show ship1, a hidden object, if you click on it and add 500 "points". So if you click on ship1 again, it will remain seen and another 500 "points" will be added. Is there a way to restrict the code so that the 500 "points" will only be added if ship1 is hidden? I was thinking of adding an if statement, but I don't know how to say a hidden object = true. Also

.show()

is part of the library I used called objectdraw.

Posted Jul 22, 2015, 11:26 PM UTC

1610
chickenmeister•

If your ship1 object has some kind of method that indicates if it is shown/hidden, you could use that. It looks like drawable objects in the "objectdraw" library have an isHidden() method, so it might look like:

if (ship1.contains(point)){ //points is the mouse location
    if (ship1.isHidden()) { // If the ship is hidden
        ship1.show();
        points= points +500;
    }
}
14
melon-breadOP•

Thanks, that solved the problem. Out of curiosity, is there a method like

(int) or (double)

for converting between void and boolean?

3
nutrecht•

Void is 'nothing'. You can't convert 'nothing' to anything. Your thinking about this the wrong way; methods return 'nothing' for a reason. If you just called "show" the object will be visible so there is no point if returning that state. If you wanted to check if the object is visible you would simply use a method (like isVisible()) to ask for it's state before setting it to the new state.

8
chickenmeister•

void is not really a data type (like int, double, etc). It is the absence of a data type. It can't be converted to another type.

In method declarations, the "void" keyword is used as a kind of placeholder when a method does not return a value. For example, look at this simple addition method:

public static int add(int a, int b) {
    int sum = a + b;
    return sum;
}

This method takes in two integer parameters, adds them, and returns the sum. You would use the method like this:

int result = add(3, 5);

Note that the value returned by the add() method is being assigned to a variable.

Now consider a different method that doesn't have any "return" value:

public static void printHello() {
    System.out.println("Hello world. ");
}

This method's return type is void. It doesn't return anything; so you can't assign its return value to a variable, or use it in any other way.

int result = printHello(); // Will not compile
6
smellyegg•

Fyi void is not a thing, void means a method returns nothing

1
CodeTinkerer•

What do you think void means? There is a specific Java meaning to void. I think you are confusing "hidden" with "void". They aren't the same.

1
BinghamL•

You could write a helper method that checks for null and returns Boolean

1
visualthoy•

Converting variable types can have unexpected consequences, you may not know how other parts of the code use that variable.

0
nutrecht•

It's simply not possible.

2
ojchase•

Can a void be converted to a boolean (or int or double as asked in comments)? No. Think of void not as a concrete return type but as an absence of a return type. Absolutely nothing is being returned, and thus there's nothing to convert, to a boolean or anything else. (Don't confuse void with null!)

Now as far as your intent goes, you need some way to track if your ship is hidden or not. u/chickenmeister provides one way to do this that works great for the objectdraw library. But what if your library doesn't have an isHidden() method available? You need some form of memory, or state, for your code to keep track of what your ships are up to. Basically you write isHidden() yourself.

I'm going to assume that ship1 is an instance of a Ship class you've written. Try adding a boolean member variable to the class and adding a isHidden() method to it. When you make the ship visible, you make sure to update the state. Check the state beforehand by calling isHidden(). Your new ship class might look something like this.

class PirateShip implements DrawableInterface{
    private boolean isHidden = true;
    void appearFromTheFog(){
        isHidden = false;
        show();
        // and anything else that happens to the ship when it appears.
        // Maybe the crew screams to try to scare other ships
    }
    boolean isHidden(){
        return isHidden;
    }
    // you may have other methods that implement DrawableInterface
}

And then your snippet above becomes

if (ship1.contains(point) && ship1.isHidden()){ //check if point in ship, and if ship is invisible
    ship1.appearFromTheFog(); // makes ship visible, and stores the state
    points= points +500;}
}
3