Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Classes and Objects
Classes
Declaring Classes
Declaring Member Variables
Defining Methods
Providing Constructors for Your Classes
Passing Information to a Method or a Constructor
Objects
Creating Objects
Using Objects
More on Classes
Returning a Value from a Method
Using the this Keyword
Controlling Access to Members of a Class
Understanding Class Members
Initializing Fields
Summary of Creating and Using Classes and Objects
Questions and Exercises
Questions and Exercises
Nested Classes
Inner Class Example
Local Classes
Anonymous Classes
Lambda Expressions
Method References
When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions
Questions and Exercises
Enum Types
Questions and Exercises
Trail: Learning the Java Language
Lesson: Classes and Objects
Home Page >Learning the Java Language >Classes and Objects
« Previous • Trail • Next »

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 longer available.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Objects

A typical Java program creates many objects, which as you know, interact by invoking methods. 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, calledCreateObjectDemo, that creates three objects: onePoint object and twoRectangle 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.getArea());        // 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 displays information about various objects. Here's the output:

Width of rectOne: 100Height of rectOne: 200Area of rectOne: 20000X Position of rectTwo: 23Y Position of rectTwo: 94X Position of rectTwo: 40Y Position of rectTwo: 72

The following three 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 when its life has ended.

« PreviousTrailNext »

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

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: Passing Information to a Method or a Constructor
Next page: Creating Objects

[8]ページ先頭

©2009-2025 Movatter.jp