Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 2800 Lake Sumter State College Mark Wilson, Instructor.

Similar presentations


Presentation on theme: "COP 2800 Lake Sumter State College Mark Wilson, Instructor."— Presentation transcript:

1 COP 2800 Lake Sumter State College Mark Wilson, Instructor

2 Birth and Death of Objects

3  The Stack All Methods Local variables First In Last Out queue  The Heap All Objects Instance variables Conceptual pile

4 Public static void main() { dostuff(); } Public void doStuff() { foo(); } Public void foo(){ bar(); } Public void bar(){ // code here }

5  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

6  Declare a reference variable Either class or interface type Thang aFooBar = new Thang();  Create an Object Object goes on the heap Thang aFooBar = new Thang();  Link object to reference variable Assign object to the reference Thang aFooBar = new Thang();

7  Looks like a method call…  Thang aFooBar = 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 public Thang (){ // there be constructor code here }

8 public class Thang { private int answerToLife; public Thang () { setAnswer(42); } public void setAnswer (int newAnswer) { answerToLife = newAnswer; } public int getAnswer () { return answerToLife; } public class doThang { public static void main (String[] args) { Thang aThang = new Thang(); System.out.println ( “The answer to life, the universe and everything” + aThang.getAnswer()); }

9 public class Thang { private int answerToLife; public Thang (int anAnswer) { setAnswer(anAnswer); } public void setAnswer (int newAnswer) { answerToLife = newAnswer; } public int getAnswer () { return answerToLife; } public class doThang { public static void main (String[] args) { Thang aThang = new Thang(42); System.out.println ( “The answer to life, the universe and everything” + aThang.getAnswer()); }

10 public class Thang { private int answerToLife; public Thang () { setAnswer(42); } public Thang (int anAnswer) { setAnswer(anAnswer); } public void setAnswer (int newAnswer) { answerToLife = newAnswer; } public int getAnswer () { return answerToLife; } public class doThang { public static void main (String[] args) { Thang aThang = new Thang(42); System.out.println ( “The answer to life, the universe and everything” + aThang.getAnswer()); }

11  Constructor Runs when new is 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

12  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

13  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

14  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

15  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()

16  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

17  Reference goes out of scope permanently  Reference is assigned to another object  Reference is explicitly set to null

18  Reference goes out of scope permanently  Reference is assigned to another object  Reference is explicitly set to null public class DuckPen{ public void foo () { bar(); } public void bar () { Duck d = new Duck(); }

19  Reference goes out of scope permanently  Reference is assigned to another object  Reference is explicitly set to null public class DuckPen{ Duck d = new Duck(); public void foo () { d = new Duck(); }

20  Reference goes out of scope permanently  Reference is assigned to another object  Reference is explicitly set to null public class DuckPen{ Duck d = new Duck(); public void foo () { d = null; }


Download ppt "COP 2800 Lake Sumter State College Mark Wilson, Instructor."

Similar presentations


Ads by Google