Java tip: Fields and methods in Java

Advanced techniques for working with fields and methods in your Java programs

The Java tutorial Java 101: Classes and objects in Java introduces the fundamentals of Java classes and objects, including how to declare classes, describe attributes via fields, describe behaviors via methods, initialize objects via constructors, and instantiate objects from classes and access their members. 

This Java tip builds on that introduction, introducing seven advanced techniques for working with fields and methods in your Java programs. Keep reading if you're ready to learn about field constants, field-access rules, chaining instance method calls, pass-by-value arguments, recursion and the method-call stack, rules for calling methods, and utility classes. 

Field constants

You can create a read-only field by including keyword final in its declaration. The result is known as a constant. For example, final int DAYS_IN_MONTH = 30; and final static double NORMAL_BODY_TEMP = 98.6; declare constants DAYS_IN_MONTH and NORMAL_BODY_TEMP. By convention, a constant's name is expressed in capital letters.

To continue reading this article register now