What is the correct name of a method which has the same name as that of class and called when we create objects?

As you know, a class
What is the correct name of a method which has the same name as that of class and called when we create objects?
provides the blueprint for objects
What is the correct name of a method which has the same name as that of class and called when we create objects?
; you create an object from a class. Each of the following statements taken from the CreateObjectDemo
What is the correct name of a method which has the same name as that of class and called when we create objects?
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
What is the correct name of a method which has the same name as that of class and called when we create objects?
class and the second and third lines each create an object from the Rectangle
What is the correct name of a method which has the same name as that of class and called when we create objects?
class.

Each statement has the following three parts:

  1. Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
  2. Instantiation: The new keyword is a Java operator that creates the object. As discussed below, this is also known as instantiating a class.
  3. Initialization: The new operator is followed by a call to a constructor. For example, Point(23, 94) is a call to Point's only constructor. The constructor initializes the new object.
The next three subsections discuss each of these actions in detail:

From the Variables
What is the correct name of a method which has the same name as that of class and called when we create objects?
section in the previous lesson, you learned that to declare a variable, you write:
type name
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.

Variables of primitive types (byte, short, int, long, char, float, double, or boolean) always hold a primitive value of that same type.

Variables of reference types, however, are slightly more complex. They may be declared in any of the following ways:

  • The declared type matches the class of the object:
    MyClass myObject = new MyClass();

  • The declared type is a parent class of the object's class:
    MyParent myObject = new MyClass();

  • The declared type is an interface which the object's class implements:
    MyInterface myObject = new MyClass();

You can also declare a variable on its own line, such as:

MyClass myObject;
When you use this approach, the value of myObject will be automatically set to null until an object is actually created and assigned to it. Remember, variable declaration alone does not actually create an object. For that, you need to use the new operator, as described in the next section.

A variable in this state, which currently references no object, is said to hold a null reference. If the code in CreateObjectDemo

What is the correct name of a method which has the same name as that of class and called when we create objects?
had declared its originOne variable in this manner, it could be illustrated as follows (variable name, plus reference pointing to nothing):

What is the correct name of a method which has the same name as that of class and called when we create objects?

A variable of reference type may also hold an object reference, as discussed in the following sections.
The new operator instantiates a class by allocating memory for a new object.

Note: The phrase "instantiating a class" means the same thing as "creating an object"; you can think of the two as being synonymous. When you create an object, you are creating an instance of a class, therefore "instantiating" a class.

The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object.

The new operator returns a reference to the object it created. Often, this reference is assigned to a variable of the appropriate type. If the reference is not assigned to a variable, the object is unreachable after the statement in which the new operator appears finishes executing.

Here'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:

What is the correct name of a method which has the same name as that of class and called when we create objects?

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:

What is the correct name of a method which has the same name as that of class and called when we create objects?

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.

This section talked about how to use a constructor.The section Providing Constructors for Your Classes

What is the correct name of a method which has the same name as that of class and called when we create objects?
explains how to write constructors for your classes.


Page 2

A typical Java program creates many objects, which as you know, interact by sending messages
What is the correct name of a method which has the same name as that of class and called when we create objects?
. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.

Here's a small program, called CreateObjectDemo

What is the correct name of a method which has the same name as that of class and called when we create objects?
, that creates three objects: one Point
What is the correct name of a method which has the same name as that of class and called when we create objects?
object and two Rectangle
What is the correct name of a method which has the same name as that of class and called when we create objects?
objects. You will need all three source files to compile this program.

public class CreateObjectDemo { public static void main(String[] args) { // declare and create a point object // and two rectangle objects Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne, 100, 200); Rectangle rectTwo = new Rectangle(50, 100); // display rectOne's width, height, and area System.out.println("Width of rectOne: " + rectOne.width); System.out.println("Height of rectOne: " + rectOne.height); System.out.println("Area of rectOne: " + rectOne.area()); // set rectTwo's position rectTwo.origin = originOne; // display rectTwo's position System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y); // move rectTwo and display its new position rectTwo.move(40, 72); System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y); } }
This program creates, manipulates, and and displays information about various objects. Here's the output:
Width of rectOne: 100 Height of rectOne: 200 Area of rectOne: 20000 X Position of rectTwo: 23 Y Position of rectTwo: 94 X Position of rectTwo: 40 Y Position of rectTwo: 72

The following sections use the above example to describe the life cycle of an object within a program. From them, you will learn how to write code that creates and uses objects in your own programs. You will also learn how the system cleans up after an object after its life has ended.


Page 3

Next, this chapter explains how to use the core classes and built-in support for character data, numeric data, and arrays. It covers the following topics:

  • Character data - either a single character or a series of characters - can be stored and manipulated by one of four classes in java.lang: Character
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    , String
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    , StringBuilder
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    , and StringBuffer
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    .
  • Numeric data - object representations of the primitive data types - are provided by the Number class and its subclasses. The Number
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    class is the superclass for all number classes in the Java platform, and its subclasses in package java.lang include Byte
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    , Double
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    , Float
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    , Integer
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    , Long
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    , and Short
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    . Additionally, the BigDecimal
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    , and BigInteger
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    classes in package java.math provide support for immutable, arbitrary-precision integer and decimal numbers.
  • Arrays - for grouping multiple values of the same type into a single object - are supported directly by the Java programming language; there is no array class. Arrays are implicit extensions of the Object
    What is the correct name of a method which has the same name as that of class and called when we create objects?
    class, so you can assign an array to a variable whose type is declared as Object.
The Java platform groups its classes into functional packages. Instead of writing your own classes to represent character, string, or numeric data, you should use the classes that are already provided by the Java platform. Most of the classes discussed in this chapter are members of the java.lang
What is the correct name of a method which has the same name as that of class and called when we create objects?
package. All classes in the java.lang package are available to your programs automatically; there is no need for you to explicitly make them available them with an import statement.


Page 4

This trail covers the fundamentals of programming in the Java programming language.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Object-Oriented Programming Concepts teaches you the core concepts behind object-oriented programming: objects, messages, classes, and inheritance. This lesson ends by showing you how these concepts translate into code. Feel free to skip this lesson if you are already familiar with object-oriented programming.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Language Basics describes the traditional features of the language, including variables, data types, operators, and control flow.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Object Basics and Simple Data Objects shows you the general principles for creating and using objects of any type. Then, this lesson describes how to use arrays, strings, and number objects, which are commonly used object types. Finally, this lesson shows you how to format data for output.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Classes and Inheritance describes how to write the classes from which objects are created.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Interfaces and Packages are features of the Java programming language that help you to organize and structure your classes and their relationships to one another.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Common Problems (and Their Solutions) explains the solutions to some problems you might run into while learning the Java language.


Page 5


You can also order our books from The Java Series Store.


Page 6

The JavaTM Tutorial
What is the correct name of a method which has the same name as that of class and called when we create objects?
What is the correct name of a method which has the same name as that of class and called when we create objects?
What is the correct name of a method which has the same name as that of class and called when we create objects?
Start of Tutorial Search
Feedback Form

This trail introduces you to the Java 2D API and shows you how to display and print 2D graphics in your Java programs. The Java 2D API enables you to easily
  • Draw lines of any thickness
  • Fill shapes with gradients and textures
  • Move, rotate, scale, and shear text and graphics
  • Composite overlapping text and graphics
For example, you could use the Java 2D API to display complex charts and graphs that use various line and fill styles to distinguish sets of data, like those shown in the following figure.

What is the correct name of a method which has the same name as that of class and called when we create objects?

The Java 2D API also enables you to store and to manipulate image data--for example, you can easily perform image-filter operations, such as blur and sharpen, as shown in the following figure.

What is the correct name of a method which has the same name as that of class and called when we create objects?

This trail covers the most common uses of the Java 2D APIs and briefly describes some of the more advanced features. For additional information about using the Java 2D APIs, see the Java 2D Programmer's Guide

What is the correct name of a method which has the same name as that of class and called when we create objects?
. Additional sample programs illustrating the Java 2D API features are also available online
What is the correct name of a method which has the same name as that of class and called when we create objects?
.

Note: The sample applets in this trail can be run with the JDK 1.2 Applet Viewer, a browser with Java Plug-in 1.2 installed, or a JDK 1.2 compatible browser.
The Java 2D APIs are closely integrated with the Abstract Windowing Toolkit (AWT). If you are not familiar with AWT, you might find it useful to review the AWT documentation, which you can download.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Overview of the Java 2D API introduces the key Java 2D concepts and describes the Java 2D rendering model.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Displaying Graphics with Graphics2D teaches you how to set up the Graphics2D rendering context to use fancy stroke and fill styles, perform transformations, clip the drawing region, composite overlapping graphics, and specify rendering preferences.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Working with Text and Fonts shows you how to use a Font object to create a font with desired attributes, and to derive a new font by changing the attributes, determine the names of the fonts that are available on your system and position text within a component.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Manipulating and Displaying Images his lesson explains how to implement double buffering and how to perform image-filter operations with BufferedImage objects.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Printing teaches you how to render 2D graphics to a printer and how to print complex documents.

What is the correct name of a method which has the same name as that of class and called when we create objects?
Solving Common 2D Graphics Problems gives the solutions to some problems you might encounter when writing 2D applets and applications.
What is the correct name of a method which has the same name as that of class and called when we create objects?
What is the correct name of a method which has the same name as that of class and called when we create objects?
What is the correct name of a method which has the same name as that of class and called when we create objects?
Start of Tutorial Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.