+ 1
can someone explain me the exact use of the "new" keyword
7 RĂ©ponses
+ 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
+ 6
the new keyword means you are simply allocating memory to the program for instantiation.
+ 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
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
0
new is allocating a space in your home when gust comes
0
New is a keyword to allocate the memory
- 4
Husam