0% found this document useful (0 votes)
59 views

JAVA Creating Objects

The document discusses how to create objects in Java by declaring a variable of a class type, instantiating the class with the new operator to allocate memory and return a reference, and initializing the object with a constructor. It also explains that declaring a reference variable alone does not create an object, the new operator must be used, and constructors have the same name as the class and no return type. Object fields are accessed within a class by name alone but outside the class an object reference followed by a dot and the field name is required.

Uploaded by

speedmail71
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

JAVA Creating Objects

The document discusses how to create objects in Java by declaring a variable of a class type, instantiating the class with the new operator to allocate memory and return a reference, and initializing the object with a constructor. It also explains that declaring a reference variable alone does not create an object, the new operator must be used, and constructors have the same name as the class and no return type. Object fields are accessed within a class by name alone but outside the class an object reference followed by a dot and the field name is required.

Uploaded by

speedmail71
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Creating Objects

As you know, a class provides the blueprint for objects; you create an object from a class.

statements has three parts (discussed in detail below):

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.
3. Initialization: The new operator is followed by a call to a constructor, which initializes the new
object

Declaring a Variable to Refer to an Object

to declare a variable, you write: type name;

 With a primitive variable, this declaration also reserves the proper amount of memory for the
variable.
 reference variable - you need to use the new operator. The new operator instantiates a class by
allocating memory for a new object and returning a reference to that memory (Instantiating a
class)
o For example: Point originOne;
o its value will be undetermined until an object is actually created and assigned to it. Simply
declaring a reference variable does not create an object.
o Point originOne = new Point(23, 94); Constructor

 The phrase "instantiating a class" means the same thing as "creating an object." When you create
an object, you are creating an "instance" of a class, therefore "instantiating" a class.
 The name of the constructor provides the name of the class to instantiate.
 The new operator returns a reference to the object it created. This reference is usually assigned to
a variable of the appropriate type, like:
 Point originOne = new Point(23, 94);
we can refer within the body of the program to any of the public members of the object rect as if they were
normal functions or normal variables, just by putting the object's name followed by a dot (.) and then the
name of the member. All very similar to what we did with plain data structures before.

The reference returned by the new operator does not have to be assigned to a variable. It can also be used
directly in an expression. For example:

int height = new Rectangle().height;


This statement creates a new Rectangle object and immediately gets its height. In essence, the
statement calculates the default height of a Rectangle. Note that after this statement has been executed,
the program no longer has a reference to the created Rectangle, because the program never stored the
reference anywhere. The object is unreferenced, and its resources are free to be recycled by the Java
Virtual Machine.

You can recognize a constructor because its declaration uses the same name as the class and it has no
return type.
The Java compiler differentiates the constructors based on the number and the type of the arguments.
Java compiler automatically provides a no-argument constructor, called the default constructor.
Using Objects
Referencing an Object's Fields

Object fields are accessed by their name. You must use a name that is unambiguous.

You may use a simple name for a field within its own class.

Code that is outside the object's class must use an object reference or expression, followed by the dot (.)
operator, followed by a simple field name, as in:

objectReference.fieldName
Attempting to use those fields exist only within an object results in a compiler error.
Objects of the same type have their own copy of the same instance fields.

You might also like