Java 9 with JShell
上QQ阅读APP看书,第一时间看更新

Understanding members composing a class

So far, we have been working with a very simple Rectangle class. We created many instances of this class in JShell and we understood how the garbage collection works. Now, it is time to pe deeper into the different members that compose a class in Java 9.

The following list enumerates the most common element types that we can include in a class definition in Java 9. Each member includes its equivalent in other programming languages to make it easy to translate our experience with other object-oriented languages into Java 9. We have already worked with a few of these members:

  • Constructors: A class might define one or more constructors. They are equivalent to initializers in other programming languages.
  • Class variables or class fields: These variables are common to all the instances of the class, that is, their value is the same for all the instances. In Java 9, it is possible to access class variables from the class and from its instances. We don't need to create a specific instance to access a class variable. Class variables are also known as static variables because they use the static modifier in their declarations. Class variables are equivalent to class attributes, class properties, and type properties in other programming languages.
  • Class methods: These methods can be invoked with the class name. In Java 9, it is possible to access class methods from the class and from its instances. We don't need to create a specific instance to access a class method. Class methods are also known as static methods because they use the static modifier in their declarations. Class methods are equivalent to class functions and type methods in other programming languages. Class methods operate on a class as a whole, and have access to class variables, class constants, and other class methods, but they don't have access to any instance members, such as instance fields or methods, because they operate at the class level with no instances at all. Class methods are useful when we want to include methods related to a class and we don't want to generate an instance to call them.
  • Constants: When we declare class variables or class fields with the final modifier, we define constants whose value cannot be changed.
  • Fields, member variables, instance variables, or instance fields: We worked with these in the previous examples. Each instance of the class has its own distinct copies of the instance fields, with their own values. Instance fields are equivalent to attributes and instance properties in other programming languages.
  • Methods or instance methods: These methods require an instance to be invoked and they can access the fields for the specific instance. Instance methods are equivalent to instance functions in other programming languages.
  • Nested classes: These classes are defined within another class. Static nested classes use the static modifier. Nested classes that do not use the static modifier are also known as inner classes. Nested classes are also known as nested types in other programming languages.