Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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,