• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quick question about creating objects

 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Could someone please explain to me why we have to create the deck object twice in order for this piece of code to work? From what I can tell, it looks as if the first Card object is only used for the 'for' condition, and is replaced by the new card object?
This is an example from my textbook. The book doesn't talk about this portion. I am not sure how to phrase this question to use google, either.

Any help would be greatly appreciated!

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeong Ryu wrote:
Could someone please explain to me why we have to create the deck object twice


You don't. You're only creating it once. Right here:

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your deck appears to be an array of cards. You create the deck on line 1, as Jeff said - but at that point, there is nothing in it.

The for loop goes through and on each iteration, creates a Card with a face and suit, and inserts it into the deck in each position.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:your deck appears to be an array of cards. You create the deck on line 1, as Jeff said - but at that point, there is nothing in it.


There are NUMBER_OF_CARDS null references. That's something. (Okay, I guess semantically it's nothing.)
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies everyone!

Sorry, I should have been more specific.


I know that line 1 for sure creates the deck object with quantity NUMBER_OF_CARDS.

On line 5, I moved my cursor on 'Card' and it was referring to my Card class. But, I am not sure why you would need to use 'new' or have to access the Card class.


What exactly is line 5 stating?
As Fred Rosenberger said, it seems that the for loop is creating, or giving values, to each elements in the deck array (0-51). However, how can you have two columns (face and suit) when the deck object is first initialized with NUMBER_OF_CARDS, which is a final value of 52? Wouldn't you have to have two different arrays for faces and suits?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line creates an array that holds NUMBER_OF_CARDS references to card objects. All the references are initially null.



This line creates one new Card object, passing values for its face and suit to the constructor, and then assigns a reference to that Card to an element of the Card array.


So each array element holds a single value--a reference that points to a Card object. That Card object in turn has to attributes: rank and suit.
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:This line creates an array that holds NUMBER_OF_CARDS references to card objects. All the references are initially null.



This line creates one new Card object, passing values for its face and suit to the constructor, and then assigns a reference to that Card to an element of the Card array.


So each array element holds a single value--a reference that points to a Card object. That Card object in turn has to attributes: rank and suit.


Ahh, I get it. That makes sense. I got confused with the syntax for objects and for arrays.
Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic