Life and Death of an ObjectLIS4930 © PICAs we learned you are responsible for constructing objects that live on the heap, and we know the heap is memory, so objects must take up memory. So why don’t we run out of memory?HeapEnter the Java Garbage Collector (gc)
Stacks and HeapsLIS4930 © PICBefore we can talk about object construction we need to first talk about where everything lives (and for how long) in Java.In Java there are two areas of memory: one we already know is the Heap and we know that is where objects live, but where does everything else live, like methods?YIP! On the stack.STACKHEAPaddToArray( )shout( )go( )bikeDogBoydoStuff( )main( )
Where do Variables live?Instance Variables belong to Objects so they are part of the Object living on the HEAPLocal Variables belong to Methods so they are part of the Method living on the STACKLIS4930 © PICBUT what if a local variable is an object?A local variable is not an object just a reference to one!public class Stacker{    public void foof(){      barf();   }   public void barf(){       Duck d = new Duck(24);   }}Duckbarf( ){         }foof( )STACKHEAP
Methods are Stacked!LIS4930 © PICWhen you call a method, the method lands on the top of a call stack.That new thing that’s pushed onto the stack is the stack frame.The stack frame remains on the stack until the closing curly brace ‘ } ‘ is reached in the method. Then the stack is removed.STACKThe method on the top of the stack is always the currently executing method.doStuff( )Look at the Stack Scenario on page 237.main( )
The Miracle of Object CreationLIS4930 © PICLet’s review how to create an object.Declare a reference variable1Dog rufus= new Dog( );Dog rufus = new Dog( );Dog rufus=new Dog( );Create an object2Link the object and the reference3
LIS4930 © PICThe Miracle of Object CreationDog rufus = new Dog( );NOPE! We’re calling the Dog constructor.Are we calling a method named Dog()? Because it sure looks like it.A constructor does look and feel a lot like a method, but it’s not a method. It’s got the code that runs when you say new. In other words, the code that runs when you instantiate an object.The only way to invoke a constructor is with the keyword new followed by the class name.
Who Constructed the Constructor?LIS4930 © PICYou can write a constructor for your class, but if you don’t Java will make one up for you, and it is called the default constructor. It looks like this:public  Dog() {//constructor code goes here}Notice anything missing?
Constructor ExampleLIS4930 © PICpublic class Duck {	public Duck( ) {System.out.println(“Quack!”);	}}A constructor lets you jump into the middle of the object creation step – into the middle of new.public class UseADuck {	Duck Todd = new Duck();}But what if we don’t know what the size of the should be?Most people use constructors to initialize the state of an object. In other words, to make and assign values to the object’s instance variables.public Duck( ) {	size = 34;}
Old vs. NewLIS4930 © PICOld ApproachNew Approach
Make it Easy to Make a DuckLIS4930 © PICRemember as soon as you create a constructor you DO NOTget given one from the JVM. Therefore, if we create a constructor that takes a parameter, you will always have to pass a parameter… Do you see an issue with this? What if the programmer doesn’t know the size of the Duck at the time of creation?What we need is to keep the default constructor AND the new constructor… In other words we need to overload our default constructor.
Overloaded ConstructorLIS4930 © PIC
Copy ConstructorsSee in-class example.LIS4930 © PIC
Notes About ConstructorsIf you create a constructor of any type you DO NOT get the default constructor from the JVM!If you write a constructor that takes arguments, and you still want a no-arg constructor, you’ll have to build the no-arg constructor yourself.If you have more than one constructor in a class, the constructor MUST have different argument lists.LIS4930 © PICLook at the different constructor examples on page 248 in your textbooks.
Constructor ReviewLIS4930 © PICA constructor is the code that runs when somebody says new on a class type:1Duck daffy = new Duck( );public Duck ( int size) { }public Duck( ) { }public Duck( ) { }public Duck(int size) { }public Duck (String name, int size) { }A constructor must have the same name as the class, and  no return type:2If you don’t put a constructor in your class, the compiler puts in a default constructor. The default constructor is always a no-arg constructor:3You can have more than one constructor in your class, as long as the argument lists are different. Having more than one constructor in a class means you have overloadedconstructors.4

12 constructors

  • 1.
    Life and Deathof an ObjectLIS4930 © PICAs we learned you are responsible for constructing objects that live on the heap, and we know the heap is memory, so objects must take up memory. So why don’t we run out of memory?HeapEnter the Java Garbage Collector (gc)
  • 2.
    Stacks and HeapsLIS4930© PICBefore we can talk about object construction we need to first talk about where everything lives (and for how long) in Java.In Java there are two areas of memory: one we already know is the Heap and we know that is where objects live, but where does everything else live, like methods?YIP! On the stack.STACKHEAPaddToArray( )shout( )go( )bikeDogBoydoStuff( )main( )
  • 3.
    Where do Variableslive?Instance Variables belong to Objects so they are part of the Object living on the HEAPLocal Variables belong to Methods so they are part of the Method living on the STACKLIS4930 © PICBUT what if a local variable is an object?A local variable is not an object just a reference to one!public class Stacker{ public void foof(){ barf(); } public void barf(){ Duck d = new Duck(24); }}Duckbarf( ){ }foof( )STACKHEAP
  • 4.
    Methods are Stacked!LIS4930© PICWhen you call a method, the method lands on the top of a call stack.That new thing that’s pushed onto the stack is the stack frame.The stack frame remains on the stack until the closing curly brace ‘ } ‘ is reached in the method. Then the stack is removed.STACKThe method on the top of the stack is always the currently executing method.doStuff( )Look at the Stack Scenario on page 237.main( )
  • 5.
    The Miracle ofObject CreationLIS4930 © PICLet’s review how to create an object.Declare a reference variable1Dog rufus= new Dog( );Dog rufus = new Dog( );Dog rufus=new Dog( );Create an object2Link the object and the reference3
  • 6.
    LIS4930 © PICTheMiracle of Object CreationDog rufus = new Dog( );NOPE! We’re calling the Dog constructor.Are we calling a method named Dog()? Because it sure looks like it.A constructor does look and feel a lot like a method, but it’s not a method. It’s got the code that runs when you say new. In other words, the code that runs when you instantiate an object.The only way to invoke a constructor is with the keyword new followed by the class name.
  • 7.
    Who Constructed theConstructor?LIS4930 © PICYou can write a constructor for your class, but if you don’t Java will make one up for you, and it is called the default constructor. It looks like this:public Dog() {//constructor code goes here}Notice anything missing?
  • 8.
    Constructor ExampleLIS4930 ©PICpublic class Duck { public Duck( ) {System.out.println(“Quack!”); }}A constructor lets you jump into the middle of the object creation step – into the middle of new.public class UseADuck { Duck Todd = new Duck();}But what if we don’t know what the size of the should be?Most people use constructors to initialize the state of an object. In other words, to make and assign values to the object’s instance variables.public Duck( ) { size = 34;}
  • 9.
    Old vs. NewLIS4930© PICOld ApproachNew Approach
  • 10.
    Make it Easyto Make a DuckLIS4930 © PICRemember as soon as you create a constructor you DO NOTget given one from the JVM. Therefore, if we create a constructor that takes a parameter, you will always have to pass a parameter… Do you see an issue with this? What if the programmer doesn’t know the size of the Duck at the time of creation?What we need is to keep the default constructor AND the new constructor… In other words we need to overload our default constructor.
  • 11.
  • 12.
    Copy ConstructorsSee in-classexample.LIS4930 © PIC
  • 13.
    Notes About ConstructorsIfyou create a constructor of any type you DO NOT get the default constructor from the JVM!If you write a constructor that takes arguments, and you still want a no-arg constructor, you’ll have to build the no-arg constructor yourself.If you have more than one constructor in a class, the constructor MUST have different argument lists.LIS4930 © PICLook at the different constructor examples on page 248 in your textbooks.
  • 14.
    Constructor ReviewLIS4930 ©PICA constructor is the code that runs when somebody says new on a class type:1Duck daffy = new Duck( );public Duck ( int size) { }public Duck( ) { }public Duck( ) { }public Duck(int size) { }public Duck (String name, int size) { }A constructor must have the same name as the class, and no return type:2If you don’t put a constructor in your class, the compiler puts in a default constructor. The default constructor is always a no-arg constructor:3You can have more than one constructor in your class, as long as the argument lists are different. Having more than one constructor in a class means you have overloadedconstructors.4