can someone explain me the exact use of the "new" keyword | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can someone explain me the exact use of the "new" keyword

19th Oct 2016, 8:41 PM
gamgee
gamgee - avatar
7 Answers
+ 6
for example you have a class Owl and in some main method of main class you want to use an Owl instance you declaring for example Owl owl = new Owl(); and you get an owl instance of Owl Class. this instance can have defined values of fields and if you create next: Owl nextOwl=new Owl(); and with some setter-methods you define nextOwl fields values you will have two innstances of one class with different values. So - "new" is a spell to create a new instance of class
19th Oct 2016, 9:03 PM
Maciej Góraj
Maciej Góraj - avatar
+ 6
the new keyword means you are simply allocating memory to the program for instantiation.
21st Oct 2016, 6:28 AM
Franky BrainBox
Franky BrainBox - avatar
+ 1
the "new" keyword allocates block of memory without giving it a name: int x; // allocate 4 bytes of memory for the variable named x new int; // allocate a part of 4 bytes of memory without giving it a name; • We can reach an object in memory using either it's name or it's address. Hence, the seconde statement is useless since no one can reach that position in memory. • How we can benefit of "new" ? one of the benefits of using "new" is when we use pointers: int *p=&x; // we can reach *p from the pointer p or directly by using it's name "x" (e.g) x=34 ≈ *p=34 int *p=new int; // we can reach *p only from the pointer name; (e.g) *p=13; // and this protects *p from being modified using x
12th Nov 2016, 3:54 PM
Nour Salman
Nour Salman - avatar
0
when I say we have bucket means I dn practically have it its a saying when I have a bucket practically that's is new is doing their it creates necessary things
21st Oct 2016, 3:05 AM
Jitu Achar
Jitu Achar - avatar
0
new is allocating a space in your home when gust comes
16th Nov 2016, 4:41 PM
Jitu Achar
Jitu Achar - avatar
0
New is a keyword to allocate the memory
18th Nov 2016, 4:38 AM
Pavankumar V
Pavankumar V - avatar
- 4
Husam
19th Oct 2016, 9:04 PM
Husam