Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,