what is = new Animal () | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

what is = new Animal ()

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

23rd Jun 2016, 12:14 PM
Ch Annus
Ch Annus - avatar
7 Answers
+ 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();
23rd Jun 2016, 3:46 PM
Sparsh Agarwal
Sparsh Agarwal - avatar
+ 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.
24th Jun 2016, 7:13 AM
Saud Ahmad
Saud Ahmad - avatar
+ 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.
23rd Jun 2016, 5:40 PM
Varun Pratap Singh
Varun Pratap Singh - avatar
+ 1
thank you for your help :)
26th Jun 2016, 3:17 PM
Ch Annus
Ch Annus - avatar
0
means that the variable will receive an object of the class animal...
28th Jun 2016, 4:14 PM
Cairo Kawamoto
Cairo Kawamoto - avatar
0
it will create a new object of a class animal Animal ape=new Animal (); here ape is the reference variable
2nd Jul 2016, 12:13 PM
AKASH VERMA
AKASH VERMA - avatar
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)
24th Jul 2016, 4:52 AM
jayesh malviya
jayesh malviya - avatar