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
Section: Classes
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.

Declaring Member Variables

There are several kinds of variables:

TheBicycle class uses the following lines of code to define its fields:

public int cadence;public int gear;public int speed;

Field declarations are composed of three components, in order:

  1. Zero or more modifiers, such aspublic orprivate.
  2. The field's type.
  3. The field's name.

The fields ofBicycle are namedcadence,gear, andspeed and are all of data type integer (int). Thepublic keyword identifies these fields as public members, accessible by any object that can access the class.

Access Modifiers

The first (left-most) modifier used lets you control what other classes have access to a member field. For the moment, consider onlypublic andprivate. Other access modifiers will be discussed later.

In the spirit of encapsulation, it is common to make fields private. This means that they can only bedirectly accessed from the Bicycle class. We still need access to these values, however. This can be doneindirectly by adding public methods that obtain the field values for us:

public class Bicycle {            private int cadence;    private int gear;    private int speed;            public Bicycle(int startCadence, int startSpeed, int startGear) {        gear = startGear;        cadence = startCadence;        speed = startSpeed;    }            public int getCadence() {        return cadence;    }            public void setCadence(int newValue) {        cadence = newValue;    }            public int getGear() {        return gear;    }            public void setGear(int newValue) {        gear = newValue;    }            public int getSpeed() {        return speed;    }            public void applyBrake(int decrement) {        speed -= decrement;    }            public void speedUp(int increment) {        speed += increment;    }}

Types

All variables must have a type. You can use primitive types such asint,float,boolean, etc. Or you can use reference types, such as strings, arrays, or objects.

Variable Names

All variables, whether they are fields, local variables, or parameters, follow the same naming rules and conventions that were covered in the Language Basics lesson,Variables—Naming.

In this lesson, be aware that the same naming rules and conventions are used for method and class names, except that

« 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: Declaring Classes
Next page: Defining Methods

[8]ページ先頭

©2009-2025 Movatter.jp