Results 1 to 2 of 2
  1. #1
    NewToJava_99 is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default 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>();

    * 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power

    Default Re: Calling method from a different class into a constructor

    Java Code:
    this.deck = Deck.loadDeck();
    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?

Similar Threads

  1. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 03:48 AM
  2. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  3. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts