constructors & garbage collection ch. 9 – head first java
TRANSCRIPT
![Page 1: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/1.jpg)
Constructors & Garbage Collection
Ch. 9 – Head First Java
![Page 2: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/2.jpg)
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
![Page 3: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/3.jpg)
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
![Page 4: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/4.jpg)
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.
![Page 5: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/5.jpg)
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
![Page 6: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/6.jpg)
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.
![Page 7: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/7.jpg)
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.
![Page 8: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/8.jpg)
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.
![Page 9: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/9.jpg)
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 10: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/10.jpg)
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.
![Page 11: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/11.jpg)
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
![Page 12: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/12.jpg)
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!!
![Page 13: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/13.jpg)
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
![Page 14: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/14.jpg)
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.
![Page 15: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/15.jpg)
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.
![Page 16: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/16.jpg)
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
![Page 17: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/17.jpg)
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.
![Page 18: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/18.jpg)
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
![Page 19: Constructors & Garbage Collection Ch. 9 – Head First Java](https://reader036.vdocuments.us/reader036/viewer/2022081808/5697bf7d1a28abf838c8475e/html5/thumbnails/19.jpg)
Math – static methods
• Math Methods– Math.random()– Math.abs()– Math.round()– Math.min()– Math.max()