Java Java Objects Meet Objects Final

Nayoon kim
Nayoon kim
3,098 Points

Question about 'instance'

Card c = new Card();

  1. I know that this sentence creates an instance (which is an object) of Card class
  2. and that instance is stored to c
  3. Card(); is the constructor.

But I'm confused which part of the sentence is the instance of Card class.. is it Card at the head bit? or the whole thing is the ... instance of Card class.

I feel like there are a lot to cover in java object but here, the course skims through all the important details.

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 55,710 Points

Hi there,

Calling the constructor together with the new keyword returns an instance of Card. This is then stored in c. The first Card, at the beginning of the expression, is used to allocate enough memory to the variable c to hold the instance of Card.

So, to the left of the equals sign, you create a Card sized hole in memory. You then create an instance/object of Card by calling the constructor with the new keyword. This returns the object which is assigned into c using the = operator.

I hope that helps,

Steve.