Category: Reference Value Assignment Conversions
-
Constants in Interfaces – Object-Oriented Programming
Constants in Interfaces A field declaration in an interface defines a named constant. Naming conventions recommend using uppercase letters, with multiple words in the name being separated by underscores. Such constants are considered to be public, static, and final. These modifiers are usually omitted from the declaration, but can be specified in any order. Such…
-
Member Declarations – Object-Oriented Programming
Member Declarations The component fields of a record class are always automatically generated based on the field components specified in the component list. Thus a record class cannot declare any new instance fields in addition to those specified in the component list. However, new instance methods can be declared in a record class. In Example…
-
Private Methods in Interfaces – Object-Oriented Programming
Private Methods in Interfaces Private methods in interfaces are no different from private methods in classes. As such, they can only be accessed inside the interface, acting as helper or auxiliary methods for non-abstract methods declared in the interface. They allow code to be shared between the non-abstract methods in the interface, thus promoting code…
-
Method Invocation Conversions Involving References – Object-Oriented Programming
5.10 Method Invocation Conversions Involving References The conversions for reference value assignment are also applicable to method invocation conversions, except for the narrowing conversion for constant expressions of non-long integer type (Table 2.17, p. 47). This is reasonable, as parameters in Java are passed by value (§3.10, p. 127), requiring that values of the actual…
-
Reference Casting and the instanceof Operator – Object-Oriented Programming
5.11 Reference Casting and the instanceof Operator In this section we explore type casting of references and the instanceof operator that can be used to perform either type comparison or pattern matching. The Cast Operator The basic form of the type cast expression for reference types has the following syntax: Click here to view code…
-
The instanceof Type Comparison Operator 2 – Object-Oriented Programming
As we have seen, the instanceof type comparison operator effectively determines whether the reference value in the reference on the left-hand side refers to an object whose class is a subtype of the type specified on the right-hand side. At runtime, it is the type of the actual object denoted by the reference on the…
-
Declaring Enum Constructors and Members – Object-Oriented Programming
Declaring Enum Constructors and Members An enum type can declare constructors and other members as in an ordinary class, but the enum constants must be declared before any other declarations (see the declaration of the enum type Meal in Example 5.25). The list of enum constants must be terminated by a semicolon (;) if followed…
-
Extending Enum Types: Constant-Specific Class Bodies – Object-Oriented Programming
Extending Enum Types: Constant-Specific Class Bodies Constant-specific class bodies define anonymous classes (§9.6, p. 521) inside an enum type—they implicitly extend the enclosing enum type creating new subtypes. The enum type Meal in Example 5.26 declares constant-specific class bodies for its constants. The following skeletal code declares the constant-specific class body for the enum constant…
-
Record Class Basics – Object-Oriented Programming
Record Class Basics A record class in Java is a special-purpose class that simplifies declaration and handling of an aggregate of values that comprise the state of a plain data object. A record class defines immutable fields and the compiler generates the get methods (also called getter or accessor methods) necessary to access the values…
-
Augmenting Basic Record Class Declaration – Object-Oriented Programming
Augmenting Basic Record Class Declaration In the rest of this section, we explore how the basic declaration of a record class can be augmented and customized. The general syntax of a record class is shown below. Click here to view code image access_modifier recordrecord_name(component_list)optional_implements_clause {optional_constructor_declarationsoptional_member_declarations} The basic declaration of a record class can be augmented with an…