1 / 20

Programming with Java

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.

megan
Download Presentation

Programming with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with Java COP 2800 Lake Sumter State College Mark Wilson, Instructor

  2. Programming with Java Birth and Death of Objects

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

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

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

  6. 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();

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

  8. 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()); } }

  9. 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()); } }

  10. 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()); } }

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

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

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

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

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

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

  17. How to Kill an Object Reference goes out of scope permanently Reference is assigned to another object Reference is explicitly set to null

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

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

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

More Related