This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Calling seperate functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling seperate functions" Watch "Calling seperate functions" New topic
Author

Calling seperate functions

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Hi All. I'm passing class ship to the GUI, using GUI(Ship ship) but can't refer to it later in the code:

Main:


GUI:


Ship:

Any ideas?

Rob.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

You need to pass the ship object to the method somehow.
Either by a field or by parameter.


Please correct my English.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
I have:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

So far so good. But don't just ignore it. Take it and store it somewhere so you can use it later.

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Thanks Paul.

Thing is i want to call the functions of each instance of class Ships. I could pass the ArrayList ships to the GUI but is there an easier way?

At the moment all that's happening is i'm calling hit on an empty ship.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
You need to distinguish passing a Ship from a List<Ship>. Both are possible, but they are very different from each other.

By an "empty ship", do you mean a null reference? You should avoid nulls as far as possible, as in the following example. Note the robust reaction to a null, and the bit about nulls in the documentation comment.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
At the moment i'm passing an empty instance of Ship to the GUI thread.

I'm using a static method to cycle through each ship in the ArrayList Ships calling the method hit() on each of them. Is there an easier way?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
What do you mean by "empty instance"?
Why are you using a static method?
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
I'm creating a ship without running setShips on it, therefore it's x and values are all zero.

I'm creating a static method hits which cycles through each member of the static arraylist ships and calls the method hits on it:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling seperate functions