+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; }

1/19/2020 6:03:50 AM

Programmer</>

12 Answers

New Answer

+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.

+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.

+2

Programmer</> I believe that would be invalid because your A class has no constructor.

+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.

+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??

0

By writing A a; you're instantiating an object of class A.

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?

0

but we know every class haa default constructor....then how we can say that class has no constructor

0

you mean ref-variable A a; has address of A class Object?? am i right

0

Programmer</> Yes I believe so.

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?

0

Programmer</> Youre not giving the node a value merely giving a variable to define when an object is instantiated