what is = new Animal () | Sololearn: Learn to code for FREE!

+4

what is = new Animal ()

what foes it do.....how does it work

6/23/2016 12:14:07 PM

Ch Annus

7 Answers

New Answer

+7

to create an object of a class, we use this syntax. for example, we create a class Mobile. to create its objects, say htc and nokia, we write Mobile htc = new Mobile(); and Mobile nokia = new Mobile();

+3

It will create a new object of class Animal and references it to a variable. Animal ani = new Animal(); ani is the reference variable here.

+1

Here u r not storing the objects reference id in a reference variable, so it is called Anonymous object. It can be used once in a life cycle. After object is used once memory is freed.

+1

thank you for your help :)

0

means that the variable will receive an object of the class animal...

0

it will create a new object of a class animal Animal ape=new Animal (); here ape is the reference variable

0

if u used malloc, function in C or new keyboard in C++ which is used to allocate memory for an object you'll understand what it does. and if u never heard about this function than let me clear, as u need to store data u need some memory , and memory is allocated by this 'new' keyword which allocated the memory according to its class definition. ( it check how space it's needs for data and for storing methods and than wrap up all the things together in memory block and return it's pointer to object. and we consider it as object as in Java)