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

12 constructors

Embed Size (px)

DESCRIPTION

 

Citation preview

LIS4930 © PIC

Life and Death of an Object

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)

LIS4930 © PIC

Stacks and HeapsBefore 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

main( )doStuff( )

go( )

shout( )

addToArray( )

Dog bike

Boy

LIS4930 © PIC

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 STACKBUT what if a local variable is

an object?public class Stacker{ public void foof(){ barf(); }

public void barf(){ Duck d = new Duck(24); }}

foof( )barf( ){ }

Duck

STACK HEAP

A local variable is not an object just a reference to one!

LIS4930 © PIC

Methods are Stacked!When you call a method, the method lands on the top of a call stack.

STACK

main( )doStuff( )

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.

The method on the top of the stack is always the currently executing method.

Look at the Stack Scenario on page 237.

LIS4930 © PIC

The Miracle of Object Creation

Let’s review how to create an object.

1 Declare a reference variable Dog rufus = new

Dog( );2 Create an object

Dog rufus = new Dog( );

3 Link the object and the reference Dog rufus = new

Dog( );

LIS4930 © PIC

The Miracle of Object Creation

Dog rufus = new Dog( );

Are we calling a method named Dog()? Because it sure looks like it.

NOPE! We’re calling the Dog

constructor.

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.

LIS4930 © PIC

Who Constructed the Constructor?

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?

LIS4930 © PIC

Constructor Example

public class UseADuck {Duck Todd = new

Duck();}

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.

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;}

But what if we don’t know what the size of the should be?

LIS4930 © PIC

Old vs. NewOld Approach New Approach

LIS4930 © PIC

Make it Easy to Make a Duck

Remember as soon as you create a constructor you DO NOT get 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.

LIS4930 © PIC

Overloaded Constructor

LIS4930 © PIC

Copy ConstructorsSee in-class example.

LIS4930 © PIC

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.

Look at the different constructor examples on page 248 in your

textbooks.

LIS4930 © PIC

Constructor Review1 A constructor is the code that runs when

somebody says new on a class type:Duck daffy = new Duck( );

2 A constructor must have the same name as the class, and no return type:public Duck ( int size) { }

3 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:public Duck( ) { }

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

public Duck( ) { }public Duck(int size) { }public Duck (String name, int size) { }