More Related Content
Similar to 13 life and scope
Similar to 13 life and scope (20)
More from Program in Interdisciplinary Computing
More from Program in Interdisciplinary Computing (20)
13 life and scope
- 1. The Role of Superclass Constructors LIS4930 © PIC Remember when you create an object it inherits the Object class automatically. FamilyDoctor object
- 2. The Role of Superclass Constructors LIS4930 © PIC All the constructors in an object’s inheritance tree must run when you make a new object. For an object to be fully-formed, all the superclass parts of itself must be fully-formed, and that’s why the super constructor must run. Object This all happens in a process called Constructor Chaining Doctor FamilyDoctor FamilyDoctor object
- 3. Demo LIS4930 © PIC In class demo – but if you’re not in class, read page 252 in the textbook.
- 4. Invoking the Superclass Constructor LIS4930 © PIC As you may have guessed, when creating a new object, the constructor automatically calls the object’s superclass constructors, as we just saw in the demo. But, if you wanted to call the superclass constructor explicitly, you could us: super( ) 1 If you don’t provide a constructor public ClassName( ) { super( ); } The compiler puts one in that looks like: 2 If you do provide a constructor but you do not put in a call to super() The compiler will put a call to super() in each of your overloaded constructors. The compiler supplied call looks like: super(); It is always like that. The compiler-inserted call to super() is always a no-arg constructor, only the no-arg one is called.
- 5. Can the child exist before the parents? LIS4930 © PIC Read page 254 in the textbook. The call to super() must be the first statement in each constructor, if you call it.
- 8. Now we know how an object is born, how long does an object live? LIS4930 © PIC An object’s life depends entirely on the life of the reference referring to it. SO if an object’s life depends on the reference variable’s life, how long does a variable live? 1 A local variable lives only within the method that declared the variable. 2 An instance variable lives as long as the object does. If the object is still alive, so are its instance variables. So the life of the reference variable is dependent on whether it is an local or instance variable.
- 9. The difference between life and scope for local variables. Life A local variable is alive as long as its Stack frame is on the Stack. Scope A local variable is in scope only within the method in which the variable was declared. When its own method calls another, the variable is alive, but not in scope until its method resumes. LIS4930 © PIC You can only use a variable when it is in scope.
- 10. What about Reference Variables? LIS4930 © PIC How does variable life affect object life? An object is alive as long as there are live references to it. But what happens when the Stack frame holding the reference gets popped off the Stack at the end of the method? If that was the only live reference to the object, the object is now abandoned on the Heap and eligible for Garbage Collection (GC). The trick is to know the point at which an object becomes eligible for garbage collection. When your program starts running low on memory, before it runs out, GC will destroy some or all of the eligible objects, to keep you from running out of RAM. Your job is to make sure that you abandon objects (i.e. make them eligible for GC) when you’re done with them.
- 11. 3 Ways to Kill an Object LIS4930 © PIC The first object is abandoned when z is ‘reprogrammed’ to a new object The first object is abandoned when z is ‘deprogrammed’ Reference ‘z’ dies at end of method void go( ) { Life z = new Life( ); } 1 3 A reference goes out of scope, permanently The reference is explicitly set to null Life z = new Life( ); z = new Life( ); 2 The reference is assigned another object Life z = new Life( ); z = null; Turn to pages 261 – 263 in your textbook for a more detailed explanation