
-----------------------------------
Albrecd
Wed Apr 23, 2008 12:33 pm

Card/Deck Object Construction
-----------------------------------
I am making a Blackjack game using Java.  Right now I'm working on the Deck and Card objects.
When my deck is instatiated, the constructor add 52 card objects to its array "CardInDeck" (13 of each suit).


public class BlackJack
{
    public static void main (String

If you take the commenting off of the output statements in Deck's constructor method, it shows that the cards have been properly initialized; however, if you instead take the commenting off of the output statements in the main program, every card is shown to have the same value (12) and the same suit (3).

If you could explain to me why this might be, I would greatly appreciate it.

Thanks

-----------------------------------
HeavenAgain
Wed Apr 23, 2008 12:54 pm

RE:Card/Deck Object Construction
-----------------------------------
private static int[] cardData = new int [2] ; i dont think it should be static but instead it should just be an instance variable and sopublic static int[] CheckCard () need to be changed too
thats probably why it will always end up as the last value you give which is 12/3?

-----------------------------------
jernst
Thu Apr 24, 2008 8:48 am

Re: Card/Deck Object Construction
-----------------------------------
Yeah that sounds right, because when you make a variable static within a class every instance of the class makes use of the same static variable. So instead of each card having its own card data, every card shares the same data the way your code had it.

-----------------------------------
Albrecd
Thu Apr 24, 2008 11:45 am

Re: Card/Deck Object Construction
-----------------------------------
Thanks for the help; it works now.  :)
