Heap versus Stack
Two main areas of memory in Java:
– (Garbage-collectible) Heap (where objects live);
– Stack (where local variables and methods, when called, live)
Local Variables & Instance Variables
Local (also known as stack) variables • Variables declared in a method and method parameters. • Temporary variables, alive only when the method they belong to is on the Stack.
Instance variables • Variables declared in a class (not inside of a method). • Live inside the object they belong to.
Methods and the Stack
• Method goes on top of the Stack when it is called and stays in the Stack until it’s done
• Stack frame: – What actually is pushed onto the Stack. – Contains the state of the method (which line of code is executing and values of all local variables).
• Method at top of the Stack is always the method being executed.
Local variables (on the Stack) – Instance variables (on the Heap)
Initialising Object State
• Using a constructor to initialise object state (i.e. instance variables): – Most common use of constructors. – Constructors: the best place to put initialisation code. – Programmers should always write a “no arguments” constructor (to build an object with default values): makes things easier for the program’s users.
Overloaded Constructors
When you have more than one constructor in a class. • Must have different argument lists (it’s the variable type and order that matters); • There may be cases where a no-arguments constructor makes no sense
Every object holds both its own declared instance variables and everything from its superclasses.
Remember: Every class in Java extends class Object (it’s the mother of all classes!).
Calling and Making Constructors
• Calling a superclass constructor: – using super() calls the super constructor; – using super() in your constructor puts the superclass on the top of the stack.
It is usually only used when we don’t want to call the parent’s no-args constructor (which means that we usually use super() with arguments).(有参构造器)
To call a constructor from another overloaded one in the same class, use this() – must be the first statement in the constructor. • A constructor can have a call to either super() or this() but not to both!
Life of Objects and Variables
• Life of an object: depends only on the life of reference variables referring to it. – Object is alive (or dead) if its reference is alive (or dead). • Variable lifetime: – same for primitive and reference variables; – different for local and instance variables.
• Life duration: – local variables: live only within the method that declared it (also referred to as being in scope); – instance variables: live for as long as object they belong to lives.
Local Variable: – Is alive as long as its Stack frame is on the Stack, i.e. until the method it belongs to completes. – Is in scope only within the method where it was declared. – Is alive (and maintains its state) but is not in scope when its own method calls another. – A (reference) variable can only be used when it is in scope.
• An object is alive as long as there are live references to it.
– If an object has only one reference to it and the Stack frame holding it gets popped off the Stack, then the object is now abandoned in the Heap.
• In this case, the object becomes eligible for Garbage Collection (GC).
Making an Object Eligible for GC
• Ways to get rid of an object’s references:
(1) The reference goes out of scope, permanently.
(2) The reference is assigned to another object.
(3) The reference is explicitly set to null.
Note: The opposite of a Java constructor is a finalizer; it can sometimes be used for the cleanup of an object.
null referrence
• Setting a reference variable to null (it means no object). (Student aStudent = null;)– If you use the dot operator on a null reference, you will get a NullPointerException error at runtime. – Unless initialised/assigned to, instance reference variables have a default value of null.



