Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

12 constructors


Published on

  • Be the first to comment

  • Be the first to like this

12 constructors

  1. 1. Life and Death of an Object<br />LIS4930 © PIC<br />As 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?<br />Heap<br />Enter the Java Garbage Collector (gc)<br />
  2. 2. Stacks and Heaps<br />LIS4930 © PIC<br />Before we can talk about object construction we need to first talk about where everything lives (and for how long) in Java.<br />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?<br />YIP! On the stack.<br />STACK<br />HEAP<br />addToArray( )<br />shout( )<br />go( )<br />bike<br />Dog<br />Boy<br />doStuff( )<br />main( )<br />
  3. 3. Where do Variables live?<br />Instance Variables belong to Objects so they are part of the Object living on the HEAP<br />Local Variables belong to Methods so they are part of the Method living on the STACK<br />LIS4930 © PIC<br />BUT what if a local variable is an object?<br />A local variable is not an object just a reference to one!<br />public class Stacker{ <br /> public void foof(){<br /> barf();<br /> }<br /> public void barf(){<br /> Duck d = new Duck(24);<br /> }<br />}<br />Duck<br />barf( ){ }<br />foof( )<br />STACK<br />HEAP<br />
  4. 4. Methods are Stacked!<br />LIS4930 © PIC<br />When you call a method, the method lands on the top of a call stack.<br />That new thing that’s pushed onto the stack is the stack frame.<br />The stack frame remains on the stack until the closing curly brace ‘ } ‘ is reached in the method. Then the stack is removed.<br />STACK<br />The method on the top of the stack is always the currently executing method.<br />doStuff( )<br />Look at the Stack Scenario on page 237.<br />main( )<br />
  5. 5. The Miracle of Object Creation<br />LIS4930 © PIC<br />Let’s review how to create an object.<br />Declare a reference variable<br />1<br />Dog rufus= new Dog( );<br />Dog rufus = new Dog( );<br />Dog rufus=new Dog( );<br />Create an object<br />2<br />Link the object and the reference<br />3<br />
  6. 6. LIS4930 © PIC<br />The Miracle of Object Creation<br />Dog rufus = new Dog( );<br />NOPE! We’re calling the Dog constructor.<br />Are we calling a method named Dog()? Because it sure looks like it.<br />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.<br />The only way to invoke a constructor is with the keyword new followed by the class name.<br />
  7. 7. Who Constructed the Constructor?<br />LIS4930 © PIC<br />You 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:<br />public Dog() {<br />//constructor code goes here<br />}<br />Notice anything missing?<br />
  8. 8. Constructor Example<br />LIS4930 © PIC<br />public class Duck {<br /> public Duck( ) {<br />System.out.println(“Quack!”);<br /> }<br />}<br />A constructor lets you jump into the middle of the object creation step – into the middle of new.<br />public class UseADuck {<br /> Duck Todd = new Duck();<br />}<br />But what if we don’t know what the size of the should be?<br />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.<br />public Duck( ) {<br /> size = 34;<br />}<br />
  9. 9. Old vs. New<br />LIS4930 © PIC<br />Old Approach<br />New Approach<br />
  10. 10. Make it Easy to Make a Duck<br />LIS4930 © PIC<br />Remember 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? <br />What if the programmer doesn’t know the size of the Duck at the time of creation?<br />What we need is to keep the default constructor AND the new constructor… In other words we need to overload our default constructor.<br />
  11. 11. Overloaded Constructor<br />LIS4930 © PIC<br />
  12. 12. Copy Constructors<br />See in-class example.<br />LIS4930 © PIC<br />
  13. 13. Notes About Constructors<br />If you create a constructor of any type you DO NOT get the default constructor from the JVM!<br />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.<br />If you have more than one constructor in a class, the constructor MUST have different argument lists.<br />LIS4930 © PIC<br />Look at the different constructor examples on page 248 in your textbooks.<br />
  14. 14. Constructor Review<br />LIS4930 © PIC<br />A constructor is the code that runs when somebody says new on a class type:<br />1<br />Duck daffy = new Duck( );<br />public Duck ( int size) { }<br />public Duck( ) { }<br />public Duck( ) { }<br />public Duck(int size) { }<br />public Duck (String name, int size) { }<br />A constructor must have the same name as the class, and no return type:<br />2<br />If 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:<br />3<br />You 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 <br />overloaded<br />constructors.<br />4<br />