1. Lesson: Language Basics
    1. Variables
      1. Naming
      2. Primitive Data Types
    2. Operator
      1. Arithmetic
      2. Relational
      3. Logical
  2. Control Flow Statements
    1. if Statement
    2. if-else Statement
    3. switch Statement
    4. Using Strings in switch Statements
    5. The while and do-while Statements
    6. The for Statement
    7. The break Statement
    8. The continue Statement
    9. The return Statement
    10. Summary of Control Flow Statements
    11. Questions and Exercises: Control Flow Statements
  3. Lesson: Classes and Objects
    1. Classes
    2. Declaring Classes
    3. Declaring Member Variables
    4. Access Modifiers
    5. Types
    6. Variable Names
    7. Defining Methods
    8. Naming a Method
    9. Overloading Methods
    10. Providing Constructors for Your Classes
    11. Passing Information to a Method or a Constructor
    12. Parameter Types
    13. Arbitrary Number of Arguments
    14. Parameter Names
    15. Passing Primitive Data Type Arguments
    16. Passing Reference Data Type Arguments
    17. Objects
    18. Creating Objects
    19. Declaring a Variable to Refer to an Object
    20. Instantiating a Class
    21. Initializing an Object
    22. Using Objects
    23. Referencing an Object's Fields
    24. Calling an Object's Methods
    25. The Garbage Collector
    26. More on Classes
    27. Returning a Value from a Method
    28. Returning a Class or Interface
    29. Using the this Keyword
    30. Using this with a Field
    31. Using this with a Constructor
    32. Controlling Access to Members of a Class
    33. Understanding Instance and Class Members
    34. Class Variables
    35. Class Methods
    36. Constants
    37. The Bicycle Class
    38. Initializing Fields
    39. Static Initialization Blocks
    40. Initializing Instance Members
    41. Summary of Creating and Using Classes and Objects
    42. Questions and Exercises: Classes
    43. Questions and Exercises: Objects
  4. Nested Classes
    1. Why Use Nested Classes?
    2. Static Nested Classes
    3. Inner Classes
    4. Inner Class Example
    5. Local and Anonymous Inner Classes
    6. Modifiers
    7. Summary of Nested Classes
    8. Questions and Exercises: Nested Classes
  5. Enum Types
    1. Questions and Exercises: Enum Types
  6. Annotations
    1. Documentation
    2. Annotations Used by the Compiler
    3. Annotation Processing
    4. Questions and Exercises: Annotations
  7. Lesson: Interfaces and Inheritance
    1. Interfaces
    2. Interfaces in Java
    3. Interfaces as APIs
    4. Interfaces and Multiple Inheritance
    5. Defining an Interface
    6. The Interface Body
    7. Implementing an Interface
    8. A Sample Interface, Relatable
    9. Implementing the Relatable Interface

3.23.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. For example, we can add a statement within the Rectangle class that prints the width and height:

System.out.println(“Width and height are: ” + width + “, ” + height);

In this case, width and height are simple names.

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:


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

System.out.println(“Width of rectOne: ” + rectOne.width);System.out.println(“Height of rectOne: ” + rectOne.height);

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 error.

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 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.

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 reference to an object. So you could use the value returned from new to access a new object’s fields:

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.

Yes No Suggest edit
Suggest Edit