Creating objects

Declaration: It is similar to a variable declaration.A variable declaration with a variable name and object type

eg: DreamCompany microsoft;

  • It does not allocate any memory unlike primitive data types

  • It just creates reference of type ‘DreamCompany’ and name ‘microsoft’ pointing to nothing

Instantiation :Allocating memory to the variable(using the ‘new’ keyword)

eg: microsoft = new DreamCompany();

  • ‘new’ operator allocates memory for the object and returns a reference to that memory

Initialization: initializing the object with some values.

microsoft.companyName =“Microsoft”;

microsoft.location =“Hyderabad”;

microsoft.packageInLacs =“25”;

We can also combine the two statements into one as follows:

DreamCompany microsoft = new DreamCompany();





Comments

Popular posts from this blog

Enhanced for - loop or for-each loop

Arrays

Constructors