wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Quick question about creating objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quick question about creating objects" Watch "Quick question about creating objects" New topic
Author

Quick question about creating objects

Jeong Ryu
Greenhorn

Joined: Sep 25, 2012
Posts: 28
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 02, 2003
Posts: 11155
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 25, 2012
Posts: 28
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 25, 2012
Posts: 28
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!
 
wood burning stoves
 
subject: Quick question about creating objects
 
Similar Threads
blackjack java
How would i shuffle my deck of cards?
Constructor problem.
can't split card shuffle method into a separate class
Need help with constructor and fields