More about Objects and Classes
4. Multiple Constructors
Answer:
The different constructors require different parameters.
(Different types of data are supplied to the constructors.)
Multiple Constructors
import java.awt.*; // import the package that contains Point class PointEg1 { public static void main ( String arg[] ) { Point a, b, c; // reference variables a = new Point(); // create a Point at (0, 0); // save the reference in "a" b = new Point( 12, 45 ); // create a Point at (12, 45); // save the reference in "b" c = new Point( b ); // create a Point containing data equivalent // to the data referenced by "b" } } |
Any of the three constructors can be used to create a Point
. It is a matter of convenience which constructor you use. Look at the example program.
To use the definition of class Point
, import the package that contains it. The statement:
import java.awt.*
says to use the AWT package that comes with Java. The *
says that everything defined in the package can be used (although this program only uses the Point
class).
Question 4:
Say that the program has just been loaded into main memory and is just about to start running.
How many reference variables are there?
How many objects are there?