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

Using Objects (The Java™ Tutorials - Learning The Java Language - Classes and Objects)

Uploaded by

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

Using Objects (The Java™ Tutorials - Learning The Java Language - Classes and Objects)

Uploaded by

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

Documentation

Search
The Java™ Tutorials Hide TOC

Classes and Objects


Classes « Previous • Trail • Next » Home Page > Learning the Java Language > Classes and Objects
Declaring Classes
Declaring Member
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no
Variables
longer available.
Defining Methods
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
Providing Constructors
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
for Your Classes
Passing Information to a
Method or a Using Objects
Constructor
Objects Once you've created an object, you probably want to use it for something. You may need to use the value of one of its fields, change one of its fields, or call one of its methods to perform an
Creating Objects action.
Using Objects
More on Classes Referencing an Object's Fields
Returning a Value from
a Method Object fields are accessed by their name. You must use a name that is unambiguous.
Using the this Keyword
You may use a simple name for a field within its own class. For example, we can add a statement within the Rectangle class that prints the width and height:
Controlling Access to
Members of a Class System.out.println("Width and height are: " + width + ", " + height);
Understanding Class
Members In this case, width and height are simple names.
Initializing Fields
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:
Summary of Creating
and Using Classes and objectReference.fieldName
Objects
Questions and Exercises For example, the code in the CreateObjectDemo class is outside the code for the Rectangle class. So to refer to the origin, width, and height fields within the Rectangle object named rectOne, the
Questions and Exercises CreateObjectDemo class must use the names rectOne.origin, rectOne.width, and rectOne.height, respectively. The program uses two of these names to display the width and the height of rectOne:

Nested Classes
System.out.println("Width of rectOne: " + rectOne.width);
Inner Class Example
System.out.println("Height of rectOne: " + rectOne.height);
Local Classes
Anonymous Classes Attempting to use the simple names width and height from the code in the CreateObjectDemo class doesn't make sense — those fields exist only within an object — and results in a compiler
Lambda Expressions error.
Method References
When to Use Nested Later, the program uses similar code to display information about rectTwo. Objects of the same type have their own copy of the same instance fields. Thus, each Rectangle object has fields
Classes, Local named origin, width, and height. When you access an instance field through an object reference, you reference that particular object's field. The two objects rectOne and rectTwo in the
CreateObjectDemo program have different origin, width, and height fields.
Classes, Anonymous
Classes, and Lambda
To access a field, you can use a named reference to an object, as in the previous examples, or you can use any expression that returns an object reference. Recall that the new operator returns a
Expressions
reference to an object. So you could use the value returned from new to access a new object's fields:
Questions and Exercises
Enum Types int height = new Rectangle().height;
Questions and Exercises
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.

Calling an Object's Methods

You also use an object reference to invoke an object's method. You append the method's simple name to the object reference, with an intervening dot operator (.). Also, you provide, within
enclosing parentheses, any arguments to the method. If the method does not require any arguments, use empty parentheses.

objectReference.methodName(argumentList);

or:

objectReference.methodName();

The Rectangle class has two methods: getArea() to compute the rectangle's area and move() to change the rectangle's origin. Here's the CreateObjectDemo code that invokes these two methods:

System.out.println("Area of rectOne: " + rectOne.getArea());


...
rectTwo.move(40, 72);

The first statement invokes rectOne's getArea() method and displays the results. The second line moves rectTwo because the move() method assigns new values to the object's origin.x and
origin.y.

As with instance fields, objectReference must be a reference to an object. You can use a variable name, but you also can use any expression that returns an object reference. The new operator
returns an object reference, so you can use the value returned from new to invoke a new object's methods:

new Rectangle(100, 50).getArea()

The expression new Rectangle(100, 50) returns an object reference that refers to a Rectangle object. As shown, you can use the dot notation to invoke the new Rectangle's getArea() method to
compute the area of the new rectangle.

Some methods, such as getArea(), return a value. For methods that return a value, you can use the method invocation in expressions. You can assign the return value to a variable, use it to
make decisions, or control a loop. This code assigns the value returned by getArea() to the variable areaOfRectangle:

int areaOfRectangle = new Rectangle(100, 50).getArea();

Remember, invoking a method on a particular object is the same as sending a message to that object. In this case, the object that getArea() is invoked on is the rectangle returned by the
constructor.

The Garbage Collector

Some object-oriented languages require that you keep track of all the objects you create and that you explicitly destroy them when they are no longer needed. Managing memory explicitly is
tedious and error-prone. The Java platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you don't have to worry about destroying
them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or,
you can explicitly drop an object reference by setting the variable to the special value null. Remember that a program can have multiple references to the same object; all references to an object
must be dropped before the object is eligible for garbage collection.

The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer referenced. The garbage collector does its job automatically when it
determines that the time is right.

« Previous • Trail • Next »


About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights

Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.
Préférences en matière de cookies | Ad Choices

You might also like