The Artima Developer Community
Sponsored Link

Objects and Java Seminar by Bill Venners
Initialization and Cleanup
Lecture Handout

Agenda


Constructors

 1 // In file initandcleanup/ex1/Account.java
 2
 3 public class Account {
 4
 5     // balance: in American pennies
 6     private long balance;
 7
 8     public Account() {
 9         balance = 0;
10     }
11
12     //...
13 }

 1 // In file initandcleanup/ex1/Example1.java
 2
 3 public class Example1 {
 4
 5     public static void main(String[] args) {
 6
 7         // Create an empty account
 8         Account acct = new Account();
 9     }
10 }

Method Overloading


Overloading and Return Value


Overloading Constructors

 1 // In file initandcleanup/ex2/Account.java
 2
 3 public class Account {
 4
 5     // balance: in American pennies
 6     private long balance;
 7
 8     public Account() {
 9         balance = 0;
10     }
11
12     public Account(long initDeposit) {
13         balance = initDeposit;
14     }
15
16     //...
17 }

 1 // In file initandcleanup/ex2/Example2.java
 2
 3 public class Example2 {
 4
 5     public static void main(String[] args) {
 6
 7         // Create an empty account
 8         Account acct1 = new Account();
 9
10         // Create an account with $ 1 Billion
11         Account acct2 = new Account(100000000000L);
12     }
13 }

Default Constructor


The this Reference


The this Constructor Invocation 


Variable Initialization


Object Initialization Order


Class Initialization Order


Array Initialization


Finalization and Cleanup


Exercises

Problem 1.

Create a class named Problem1 that has a no-arg constructor that prints out the message "No-arg constructor invoked!". Add a main() method in class Problem1 that creates an instance of the Problem1 class.

Problem 2.

Create a class named Problem2 that has two constructors: a no-arg constructor and a constructor that takes a String as its only input parameter. Have the no-arg constructor print out the message "No-arg constructor invoked!". Have the other constructor do two things: first, invoke the no-arg constructor with this(); second, print out the String passed in as the input parameter. Add a main() method in class Problem2 that creates an instance of the Problem2 class using the constructor that takes a String. To the constructor, pass in the String, "String constructor invoked!".

Problem 3.

Create a Java application named Problem3 that creates an array of type Problem1 and length five. Then initialize the first three members of the array with references to three separate instances of class Problem1.

Problem 4.

Create a Java application named Problem4 that has a private static float variable named coefficient. Place the coefficient variable first in the class. Second in the class, place a main() method that prints out the value of coefficient. Third in the class, place a static initialization block that sets coefficient to 2.0.

Problem 5.

Create a Java application named Problem5 that creates a two dimensional array of ints named lincoln. Initialize each array element with the sum of its indices. For example, lincoln[1][2] should be initialized to 1 + 2, or 3. Print out all the values stored in the lincoln array.

Sponsored Links

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us