Java Reference
In-Depth Information
constructor, the JVM will supply a default constructor (also takes no
arguments) automatically.
setMessage(String) . This accessor method begins with the
word set . It takes one parameter. It specifies the message to be re-
turned by the corresponding get method.
getMessage() . This accessor method returns the currently-
defined message. In our example, we choose to uppercase the mes-
sage.
Note Accessor methods are used in JavaBeans classes to access any privately de-
clared class members. In this case, the private variable identified as message can be
accessed using these methods. Accessor methods are more commonly referred to as
“getters” and “setters.”
Methods beginning with set and get are termed as setter and getter methods.
The variable message is private to the class, which means you have no direct access
to message from outside of the class.
You'll see the keyword this used in the class. It is a special keyword in Java used
to reference the current object. Its use is redundant in Listing 1-1 , but would be needed
if any of the methods happened to create variables of their own that were also named
message .
It is common in Java to mediate access to class variables through setter and getter
methods like those in our example. Those methods represent a contract of sorts with
other classes and your main program. Their benefit is that you can change the storage
implementation of HelloMessage however you like. Other code that depends upon
HelloMessage will continue to work properly so long as you maintain the external
behavior of setMessage() and getMessage() .
The Main Program
The incantation public static void main(...) is used from within a public
class to denote the beginning point of a Java program. That declaration begins an ex-
ecutable method named main . You must specify one parameter that is an array of
strings, and typically that parameter is defined as String[] args .
Search WWH ::




Custom Search