+ 2

Hello Everyone.plz tell me what actually happens in memory when we create refernce variable without creating object of class.

just like class A { } public class Checks { public static void main(String[] args) { A a; }

19th Jan 2020, 6:03 AM
Wasiq Abdullah
12 odpowiedzi
+ 7
a is assigned a stack memory location large enough to hold a pointer to the heap that could store class A. It is not initialized so points to garbage. Attempting to read it generates an error message. After writing to it with new A() or other initialization, you can read it and access the heap memory stored there.
20th Jan 2020, 7:03 AM
John Wells
John Wells - avatar
+ 3
They both would be allocated stack space to hold the pointer. If initialized, that pointer is the link to the heap space allocated for the instance.
20th Jan 2020, 7:22 AM
John Wells
John Wells - avatar
+ 2
Programmer</> I believe that would be invalid because your A class has no constructor.
19th Jan 2020, 3:35 PM
George Ryan
George Ryan - avatar
+ 1
Programmer</> Ah, well your question is with memory. Using the logic of having a default constructor what happens in memory is essentially A a; sets aside memory for your object. Then setting a= new A; it fills that memory with your object's value.
19th Jan 2020, 3:39 PM
George Ryan
George Ryan - avatar
+ 1
John Wells my another ques is that what reallyy happens in memory when we create object of main class inside its main () function...& what happens in memory when we create object of nested class of main class inside its main() method??
20th Jan 2020, 7:16 AM
Wasiq Abdullah
0
By writing A a; you're instantiating an object of class A.
19th Jan 2020, 3:20 PM
George Ryan
George Ryan - avatar
0
George Ryan but what if i dont create object that class. like A a=new A (); and only create reference variable a .then what will happen in memory?
19th Jan 2020, 3:33 PM
Wasiq Abdullah
0
but we know every class haa default constructor....then how we can say that class has no constructor
19th Jan 2020, 3:37 PM
Wasiq Abdullah
0
you mean ref-variable A a; has address of A class Object?? am i right
19th Jan 2020, 3:41 PM
Wasiq Abdullah
0
Programmer</> Yes I believe so.
19th Jan 2020, 3:42 PM
George Ryan
George Ryan - avatar
0
https://code.sololearn.com/crP73lVS4tH6/?ref=app....right plz help me in LinkedList program. here in line no 12..why we created ref-variable first of Node (Inner) class outside the i mean in Linked List....my question is that why we can created Node first; inside node class?
19th Jan 2020, 3:48 PM
Wasiq Abdullah
0
Programmer</> Youre not giving the node a value merely giving a variable to define when an object is instantiated
19th Jan 2020, 3:50 PM
George Ryan
George Ryan - avatar