Which method is automatically executed when an instance of the class is created in memory?
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 CreateObjectDemo program creates an object and assigns it to a variable:Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne, 100, 200); Rectangle rectTwo = new Rectangle(50, 100);The first line creates an object from the Point class and the second and third lines each create an object from the Rectangle class. Declaring a Variable to Refer to an ObjectFrom the Variables section in the previous lesson, you learned that to declare a variable, you write:type nameThis 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. 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: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; } }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:Point originOne = new Point(23, 94);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: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; } }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:Rectangle rectOne = new Rectangle(originOne, 100, 200);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 rectTwo = new Rectangle(50, 100);The Rectangle constructor used in the following statement doesn't take any arguments, so it's called a no-argument constructor:Rectangle rect = new Rectangle();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.
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved. Which method is automatically invoked when an object of a class is created?A constructor in C++ is a special method that is automatically called when an object of a class is created.
Which is a method that is called when a new instance of a class is created?In C++, the creation of a new instance of the class is called instantiation. Memory is allocated for that object and the class constructor runs. Programmers can instantiate objects on the heap with a new keyword or on the stack as a variable declaration.
Which method is automatically invoked when an object of a class is created in Python?Also every class in python contains a special method called initializer (also commonly known as constructors), which get invoked automatically every time new object is created.
Which method is automatically called when you pass an object as an argument?A class method is similar to an instance method, but it has a class object passed as its first argument. Recall that, when an instance method is called from an instance object, that instance object is automatically passed as the first argument to the method.
|