What must be true about any method that instantiates the book class with this constructor?
The JavaTM Tutorial
Trail: Learning the Java Language Lesson: Object Basics and Simple Data Objects As you know, a class provides the blueprint for objects ; you create an object from a class. Each of the following statements taken from the Declaring a Variable to Refer to an ObjectFrom the Variables section in the previous lesson, you learned that to declare a variable, you write:This notifies the compiler that you will use name to refer to data whose type is type. The Java programming language divides variable types into two main categories: primitive types, and reference types.type name Instantiating a ClassThe new operator instantiates a class by allocating memory for a new object. Initializing an ObjectHere's the code for the Point class:This class contains a single constructor. You can recognize a constructor because it has the same name as the class and has no return type. The constructor in the Point class takes two integer arguments, as declared by the code (int x, int y). The following statement provides 23 and 94 as values for those arguments:public class Point { public int x = 0; public int y = 0; //A constructor! public Point(int x, int y) { this.x = x; this.y = y; } }The effect of the previous line of code can be illustrated in the next figure: Here's the code for the Rectangle class, which contains four constructors:Point originOne = new Point(23, 94);Each constructor lets you provide initial values for different aspects of the rectangle: the origin; the width, and the height; all three; or none. If a class has multiple constructors, they all have the same name but a different number of arguments or different typed arguments. The Java platform differentiates the constructors based on the number and the type of the arguments. When the Java platform encounters the following code, it knows to call the constructor in the Rectangle class that requires a Point argument followed by two integer arguments:public class Rectangle { public int width = 0; public int height = 0; public Point origin; //Four constructors public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p) { origin = p; } public Rectangle(int w, int h) { this(new Point(0, 0), w, h); } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } //A method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } //A method for computing the area of the rectangle public int area() { return width * height; } }This call initializes the rectangle's origin variable to the Point object referred to by originOne. The code also sets width to 100 and height to 200. Now there are two references to the same Point object; an object can have multiple references to it, as shown in the next figure: The following line of code calls the constructor that requires two integer arguments, which provide the initial values for width and height. If you inspect the code within the constructor, you will see that it creates a new Point object whose x and y values are initialized to 0:Rectangle rectOne = new Rectangle(originOne, 100, 200);The Rectangle constructor used in the following statement doesn't take any arguments, so it's called a no-argument constructor:Rectangle rectTwo = new Rectangle(50, 100);If a class does not explicitly declare any constructors, the Java platform automatically provides a no-argument constructor, called the default constructor, that does nothing. Thus, all classes have at least one constructor.Rectangle rect = new Rectangle();
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved. When Deserializing an object using the readObject method you must cast the return value to the desired class type?When deserializing an object using the readObject method, you must cast the return value to the desired class type. When the code in a try block may throw more than one type of exception, you need to write a catch clause for each type of exception that could potentially be thrown.
When you write a constructor for a class it still has the default constructor that Java automatically provides?When you write a constructor for a class, it still has the default constructor that Java automatically provides. A class may not have more than one constructor. To find the classes needed for an object-oriented application, you identify all of the verbs in a description of the problem domain.
When you write a method that throws a checked exception you must?If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using the throws keyword. In checked exception, there are two types: fully checked and partially checked exceptions.
When an exception is thrown by a method that is executing?8) When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.
|