Your SlideShare is downloading. ×
0
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
12 constructors
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
432
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Life and Death of an Object
    LIS4930 © PIC
    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?
    Heap
    Enter the Java Garbage Collector (gc)
  • 2. Stacks and Heaps
    LIS4930 © PIC
    Before 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.
    STACK
    HEAP
    addToArray( )
    shout( )
    go( )
    bike
    Dog
    Boy
    doStuff( )
    main( )
  • 3. Where do Variables live?
    Instance Variables belong to Objects so they are part of the Object living on the HEAP
    Local Variables belong to Methods so they are part of the Method living on the STACK
    LIS4930 © PIC
    BUT 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);
    }
    }
    Duck
    barf( ){ }
    foof( )
    STACK
    HEAP
  • 4. Methods are Stacked!
    LIS4930 © PIC
    When 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.
    STACK
    The 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 of Object Creation
    LIS4930 © PIC
    Let’s review how to create an object.
    Declare a reference variable
    1
    Dog rufus= new Dog( );
    Dog rufus = new Dog( );
    Dog rufus=new Dog( );
    Create an object
    2
    Link the object and the reference
    3
  • 6. LIS4930 © PIC
    The Miracle of Object Creation
    Dog 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 the Constructor?
    LIS4930 © PIC
    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:
    public Dog() {
    //constructor code goes here
    }
    Notice anything missing?
  • 8. Constructor Example
    LIS4930 © PIC
    public 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. New
    LIS4930 © PIC
    Old Approach
    New Approach
  • 10. Make it Easy to Make a Duck
    LIS4930 © PIC
    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?
    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. Overloaded Constructor
    LIS4930 © PIC
  • 12. Copy Constructors
    See in-class example.
    LIS4930 © PIC
  • 13. Notes About Constructors
    If 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 © PIC
    Look at the different constructor examples on page 248 in your textbooks.
  • 14. Constructor Review
    LIS4930 © PIC
    A constructor is the code that runs when somebody says new on a class type:
    1
    Duck 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:
    2
    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:
    3
    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
    overloaded
    constructors.
    4

×