-2

This may be a dumb question although due to my poor wording i'm having a hard time getting a good understanding from searching up on it. When instantiating an object why is the declared like so.

Object @object = new Object();
// rather than just
Object @object;

Is this simply just to call the objects constructor or is there something i'm missing?

Darjan Bogdan
  • 3,780
  • 1
  • 22
  • 31
C. Dodds
  • 325
  • 3
  • 12
  • 1
    Because until you give it an instance of a class it is only a null shaped version of the class. – BugFinder Jun 12 '18 at 06:58
  • 2
    Think of the `@object` variable as a post-it note, and `Object` as a house. This: `Object @object;` only gives you a blank post-it note. This: `new Object()` builds a house, and this: `... = ...` assigns the address of the house to the post-it note, which gives you a post-it note with an address on it. So your first statement constructs the object, declares the variable, and assigns the address of the object (the reference to it) to the variable. Your second statement only declares a variable, as of yet it has no value. – Lasse V. Karlsen Jun 12 '18 at 07:02

1 Answers1

0

Because thats the syntax to make it a new instance.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321