Here:
Emp e=new Emp();
e
is reference variable which holds the address of an object which is created in heap area.
Reference id gets generated in form of a hash code, with help of magic method of object (object has total 9 methods) that is toString()
method; internally, using the toString
method of the object, it automatically generates Ref id for each object created at run time.
Memory for object always gets reserved in Heap area of RAM. bcz of there is no explicit pointer available in java. Here, ref variable points to the stack to give reference to object to reserve memory in Heap; bcz object also don't know where the memory is.
Stack segment is also part of memory: when we give ref variable then ref variable must be already stored in stack then ref variable knows which part of memory of head is free and it points to object reserve place and it hold in reference variable.
'new' is a textable operator which helps to create object.
Emp()
: is child class of class; if there no constructor provided in java program by programmer explicitly, then the compiler adds a default constructor implicitly, and reason to give the child class name as same class name is that so where "new" can easily knows how much memory is required for non static data members.