Results 1 to 11 of 11
- 01-16-2010, 07:45 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
How to run method from a different object?
Hi,
I am making a simple card game and am having a problem running a method from a different object.
I have a class called decks which has a method call nextCard, I want to run this method from a class called dealer.
I understand the idea and think I am just stuck with the syntax.
When I create a new instance of dealer I am able to select an instance of decks so all the information is inherited. But nextCard in dealer doesn't compile with the error "incompatible types - found void but expected decks".Java Code:public class dealer { private decks decks; /** * Constructor for objects of class dealer */ public dealer(decks newDeck) { decks = newDeck; } /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ public void nextCard() { decks nextCard=decks.nextCard(); } }
Any help will be much appreciated.
- 01-16-2010, 08:15 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
-
I may be reading it wrong, but I don't see the code for the nextCard method of the deck class. Rather I see a similarly named method from the 'dealer' class. I'm not sure what deck's method will return, but I'm hoping that it will be a Card. Regardless, I agree, that it is very unlikely to return a deck object, because if it does, this program is completely borked.
- 01-16-2010, 08:28 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 01-16-2010, 08:32 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
Thanks for the help but I am not quite getting this.
In the decks class I have a method called nextCard that uses system.out.println to print the next card in the deck.
All I want to do is call this method from from the dealer class. Is this even possible?
-
-
- 01-16-2010, 08:46 PM #8
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
-
You've got a lot of reading and studying to do.
You're doing this:
which calls deck's nextCard method but tries to set the result returned (here void or nothing) into a decks object called nextCard. Dont' do that. If you want to just call the method, then just call the method.Java Code:decks nextCard = decks.nextCard();
Incidently, your class decks and variable decks have the same name which is very bad form. The class should be capitalized, Decks. If it were my program, I'd make it singular Deck (and the variable deck).Java Code:decks.nextCard();
- 01-16-2010, 09:01 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Calling a method (on an object) in another class basically runs as follows:
This is just a silly example but it shows how to call a method on an object from another class; all the object needs is a reference to that other object.Java Code:public class AClass { // constructor and other stuff here ... // everybody can call this method: public int aMethod(int x) { // a silly example method return x+41; } } // ... public class AnotherClass { private AClass anObject; public AnotherClass(AClass object) { // contructor anObject= object; // keep a reference to the other thing } // ... public void method() { System.out.println(anObject.aMethod(1)); // call method in other class } }
kind regards,
JosLast edited by JosAH; 01-16-2010 at 09:04 PM.
- 01-16-2010, 09:04 PM #11
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
returning an object from a method
By bigj in forum New To JavaReplies: 7Last Post: 01-08-2010, 12:39 PM -
Calling Object.method
By Fedor in forum New To JavaReplies: 1Last Post: 04-11-2009, 01:44 PM -
Return an object to use in another method
By TidusSolan in forum New To JavaReplies: 3Last Post: 03-19-2009, 08:00 PM -
[SOLVED] Non-synchronized instance method of an Object
By piyu.sha in forum Threads and SynchronizationReplies: 2Last Post: 10-06-2008, 06:35 AM -
Calling a method for all instances of an object
By rattle in forum New To JavaReplies: 4Last Post: 04-30-2008, 02:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks