200 likes | 297 Views
Programming with Java . COP 2800 Lake Sumter State College Mark Wilson, Instructor. Programming with Java . Birth and Death of Objects. The Stack and The Heap. The Stack All Methods Local variables First In Last Out queue. The Heap All Objects Instance variables Conceptual pile.
E N D
Programming with Java COP 2800 Lake Sumter State College Mark Wilson, Instructor
Programming with Java Birth and Death of Objects
The Stack and The Heap • The Stack • All Methods • Local variables • First In Last Out queue • The Heap • All Objects • Instance variables • Conceptual pile
The Stack Public static void main() { dostuff(); } Public void doStuff() { foo(); } Public void foo(){ bar(); } Public void bar(){ // code here }
Bullet Points • Java has two main areas of memory: • Stack • Heap • Instance variables are declared • Inside a class but, • Outside any method • Instance variables live on the heap • Local variables are declared • Inside a method • Inside a method parameter • Local variables live on the stack • Object reference variables work like primitive variables • Objects live on the heap regardless of where the reference variable lives
Object Creation • Declare a reference variable • Either class or interface type • ThangaFooBar= new Thang(); • Create an Object • Object goes on the heap • ThangaFooBar = new Thang(); • Link object to reference variable • Assign object to the reference • ThangaFooBar= new Thang();
Constructor public Thang (){ // there be constructor code here } • Looks like a method call… • ThangaFooBar = new Thang(); • But it’s not… It’s a constructor • Every class has a constructor • Supplied by the programmer • Supplied by the compiler • Runs before object is assigned to a reference variable • No return type • Must have the same name as the class
Initialize States public class Thang { private intanswerToLife; public Thang () { setAnswer(42); } public void setAnswer (intnewAnswer) { answerToLife = newAnswer; } public intgetAnswer () { return answerToLife; } } public class doThang { public static void main (String[] args) { ThangaThang = new Thang(); System.out.println ( “The answer to life, the universe and everything” + aThang.getAnswer()); } }
Runtime Initialization public class Thang { private intanswerToLife; public Thang (intanAnswer) { setAnswer(anAnswer); } public void setAnswer (intnewAnswer) { answerToLife = newAnswer; } public intgetAnswer () { return answerToLife; } } public class doThang { public static void main (String[] args) { ThangaThang = new Thang(42); System.out.println ( “The answer to life, the universe and everything” + aThang.getAnswer()); } }
Overloaded Constructor public class Thang { private intanswerToLife; public Thang () { setAnswer(42); } public Thang (intanAnswer) { setAnswer(anAnswer); } public void setAnswer (intnewAnswer) { answerToLife = newAnswer; } public intgetAnswer () { return answerToLife; } } public class doThang { public static void main (String[] args) { ThangaThang = new Thang(42); System.out.println ( “The answer to life, the universe and everything” + aThang.getAnswer()); } }
Remember • Constructor • Runs when newis used on a class type • Must have the same name as the class • Can’t have a return type • Initializes state • May be overloaded • Instance variables are initialized 0/0.0/false/null by default
Overloaded Constructors Compiler adds a default (no arg) constructor If you add a constructor, the compiler won’t add the default Always add a default constructor and supply reasonable default values Overloaded constructors means there is more than one Overloaded constructors must have different argument lists Two constructors can’t have the same argument list Argument list includes order and types
Constructors and Superclasses Objects inherit everything from its superclasses Object gets space for all the inherited elements All the constructors in an object’s inheritance tree must run when the object is created Process of calling parent constructors is called constructor chaining
Super Constructor super() puts the superclass constructor on the stack super() must be the first call in the constructor super() is added by the compiler if it isn’t there super() is added to each overloaded constructor if it isn’t there super() can include arguments to invoke overloaded constructors in the superclass
Invoke Overloaded Constructor this() invokes an overloaded constructor from another constructor this() refers to the current object constructors this() can only be called from a constructor this() must be the first statement in a constructor this() can’t be used in a constructor that calls super()
Life of an Object • Depends on the life of the reference variable • Local variable only lives within the time the method is on the stack • Instance variable lives as long as it’s object does • Life is not the same as scope • Object lives as long as there is a live reference to it • Unreferenced objects may continue to exist • Can’t be accessed • Candidate for garbage collection
How to Kill an Object Reference goes out of scope permanently Reference is assigned to another object Reference is explicitly set to null
How to Kill an Object public class DuckPen{ public void foo () { bar(); } public void bar () { Duck d = new Duck(); } } Reference goes out of scope permanently Reference is assigned to another object Reference is explicitly set to null
How to Kill an Object public class DuckPen{ Duck d = new Duck(); public void foo () { d = new Duck(); } } Reference goes out of scope permanently Reference is assigned to another object Reference is explicitly set to null
How to Kill an Object public class DuckPen{ Duck d = new Duck(); public void foo () { d = null; } } Reference goes out of scope permanently Reference is assigned to another object Reference is explicitly set to null