constructors & garbage collection ch. 9 – head first java

Post on 20-Jan-2016

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Constructors & Garbage Collection

Ch. 9 – Head First Java

Review from 1st part of Ch. 9

• Java has 2 areas of memory – stack & heap• Instance variable declared inside a class, outside of a

method• Local variables declared inside a method or method

parameter• All local variables live on the stack, in the frame

corresponding to the method declared• Object reference variables work like primitive

variables• All objects live in the heap

The Miracle of Object Creation

• 3 Steps– Declare a Reference Variable– Create an Object (miracle)– Link the Object and the Reference.

• Duck myDuck = new Duck () Constructor• Looks like a method, but it isn’t• The code that runs when you say new.• The code that runs when you instantiate

(create) an object

Constructors

• Only way to invoke a constructor is with the keyword new, followed by the Class name

• Every class you create has a constructor, even if you don’t write it yourself– The compiler writes one for you!!

• The default constructor is always a no-arg constructor.

Construct a Duck

• Constructor runs before the object can be assigned to a reference

• You get a chance to step in and do things to get the object ready for use

Initializing the state of a new Duck• Most people use constructors to initialize the

state of an object• Make a constructor with Arguments – p. 244-

245

Overloaded Constructors

• More than one constructor in a class• To compile, each constructor must have a

different argument list• You can have two constructors that have

identical types, as long as the order is different.

Bullet Points

• Page 247!! Page 249 – four points:– A constructor is the code that runs when someone

says new on a class type– A constructor must have the same name as the

class, and no return type– If you don’t put a constructor in your class, the

compiler puts in a default constructor.– You can have more than one constructor in your

class; as long as arguments are different; overloaded constructor.

Superclasses & Constructors

• When an object is created, the object gets space for all the instance variables from all the way up the inheritance tree

• It’s almost as if multiple objects materialize.

• The object created has layersof itself representing eachsuperclass.

Role of Superclass Constructors in an Object’s Life

• All the constructors in an object’s inheritance tree must run when you make a new object.

• Saying “new” is a Big Deal. – Starts the constructor chain reaction.

• When a constructor runs, it immediately calls its superclass constructor, all the way up the tree to the Object constructor.

• Process is called Constructor Chaining

Page 252

• Output example & stack when objects are called.• Sharpen your pencil; • How do you invoke a superclass constructor?• Only way to call is by calling super ()• Puts the superclass constructors on the top of the

stack.• If you don’t put one in, the compiler does it for

you.

Order of things; Parent 1st child next

• Superclass of an object have to be fully formed (completely built) before the subclass parts can be constructed.

• Each subclass constructor immediately invokes its own superclass constructor until the Object constructor is on the top of the Stack.

• The call to super() must be the first statement in each constructor.

• Examples – bottom of page 254

Superclass Constructors with Arguments

• You can pass arguments into the super() call

• Invoking one overloaded constructor from another– Use this() to call a constructor from another

overloaded constructor in the same class– Every constructor can have a call to super() or

this(), but never both!!

How long does an object live?

• Depends on the life of references that refer to it.

• If the reference is alive, the object is too• How long does a variable live– Depends on whether the variable is a local

variable or an instance variable

Local v. Instance Variables

• A local variable lives only within the method that declared the variable

• An instance variable lives as long as the object does. If the object is still alive, so are its instance variables.

Difference between life and scope for local variables

• Life – a variable is alive as long as its Stack frame is on the stack; until the method completes

• Scope – A local variable is in scope only within the method in which the variable was declared. You can use a variable only when it is in scope.

What about reference variables?

• Rules for primitives & references – the same• A reference variable can only be used when it’s

in scope; you can’t use an object’s remote control unless you’ve got a reference variable that’s in scope.

• An object is alive as long there are live references to it.

• Trick – to know the point at which an object is eligible for garbage collection

Three ways to get rid of an object’s reference

1. The reference permanently goes out of scope 2. The reference is assigned another object3. The reference is explicitly set to null.

Static Methods

• Public static int min(int a, int b) {– No objects in a static method

• Math methods - don’t use any instance variable values

• All you need is the math “class”• Don’t need to have an “instance” of Math

Math – static methods

• Math Methods– Math.random()– Math.abs()– Math.round()– Math.min()– Math.max()

top related