Results 1 to 2 of 2
- 11-27-2011, 08:01 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Calling method from a different class into a constructor
I would appreciate it if anyone could offer some assistance or advice on how to call a method from one class into a constructor of another. This is for a homework assignment and i have completed most of it but i an getting really stuck on this simple concept and cannot move forward without it. I am sure it is something simple or obvious that i have done wrong but being new to java i just cannot figure it out.
I have a class called Deck which i load a number of cards into, the code below is the basics of it:
/** The number of times to shuffle */
public static final int TIMES_TO_SHUFFLE = 1000;
private ArrayList<Card> deck;
/**
* Constructor for objects of class Deck
*/
public Deck()
{
deck = new ArrayList<Card>();
loadDeck();
}
/**
* Load a deck with all the cards
*/
public void loadDeck()
{
addCard(new Card("Ace", "Spades", 1));
addCard(new Card("Ace","Clubs" , 1));
addCard(new Card("Ace","Diamonds",1));
addCard(new Card("Ace", "Clubs" , 1));
addCard(new Card("King","Spades" ,10));
addCard(new Card("King", "Clubs" , 10));
addCard(new Card("King","Diamonds",10));
addCard(new Card("King","Hearts",10));
}
I now have have another class called Game and would like to call the loadDeck() method into the contructor of the Game class, however i am getting a compile error: "Load static method loadDeck() cannot be referenced from a static context. Here are the basics of the code.
public class Game
{
public static final int ACE_LOW_VALUE = 1;
public static final int ACE_HIGH_VALUE = 11;
public static final int BLACKJACK = 21;
public static final int INITIAL_CARD_DEAL = 2;
private Deck deck;
private InputReader reader;
private ArrayList<Card> hand;
/**
* Constructor for objects of class Game
*/
public Game(Deck newDeck)
{
hand = new ArrayList<Card>();
this.deck = Deck.loadDeck();
}
So could anyone please tell what is that i am doing wrong and suggest a better\correct way of doing this ?
Thank you
- 11-27-2011, 11:00 PM #2
Re: Calling method from a different class into a constructor
You are attempting to call the loadDeck method as if it were static and returned a Deck object. But it is not static and returns void. So you cannot do that. Why are you trying to do that anyway? The loadDeck method is called from the Deck constructor. So everytime you create a Deck instance the cards are loaded. Since the Game constructor has a Deck as a parameter then a Deck has been created and the cards loaded. I fail to see why you are trying to do it again. Why does the Game constructor have a Deck parameter which you totally ignore?Java Code:this.deck = Deck.loadDeck();
Similar Threads
-
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks