Defining a Class
So right after the "public" keyword, we see the "class" keyword.
The class keyword is used to specify that the following statement block defines a class.
The class can be used to instantiate an object that is defined by the class.
In our example case, we are defining a class called "Announcer".
By the way, a class is usually defined in a ".java" file that is compiled into bytecode by a compiler into a ".class" file.
Typically, your .java and .class filenames will be equivalent to the class name.
Thus, the Announcer class would be stored in a file called Announcer.java and be compiled into a file named Announcer.class.
Once the class is specified, the class is defined.
To define a class you'll remember, you simply define its properties and methods.
In our case, we have one property called _announcements and several methods including:
Announcer(), Announcer(), setAnnouncement(), getAnnouncement(), and printAnnouncement()
Construction
Hey wait a minute...what is the Announcer() method and why are there two of them?
Well, the Announcer() method is a very special method called a "Constructor".
The constructor method, that always has the same name as the class file, is used to "construct" an actual object out of the class.
When an object is instantiated this method is called to initialize the object.
Once it is called, it will never be called again however.
So you should only put initialization code here.
So why are there two of them?
Well this is an example of polymorphism.
The difference between the two versions of Announcer() is that one of them takes no arguments and the other one takes a single String as an argument.
In the first case, an Announcer object will be created with the announcement of "Hello Cyberspace!" and in the second case, it will be created with some other phrase determined by the object that instantiated the Announcer!
Developing an API
After the definition of the constructors, you'll notice the methods printAnnouncement(), getAnnouncement() and setAnnouncement().
These methods represent the public API of this class and can be called upon by other objects.
They allow other objects in the "object space" to work with the Announcer object.
In the case of the printAnnouncement and setAnnouncement() outside objects are given the ability to ask the Announcer object to do things.
In the case of the getAnnouncement, outside objects are able to ask the Announcer for some piece of data.
As you can see, if a method is required to return some value, the value of the return type is specified in the definition of the method and the method is concluded by returning a value of that type.
In this case, the getAnnouncement() method returns the string contained in the variable _announcement.
NEXT
Selena Sol contributes to the JavaBoutique's Introduction to Java. Selena curently works for Barclays Capital in London, one of the leading global investment banks in Europe and has worked as a software developer for the National Center for Human Genome research, Microline Software, Neuron Data, and Electric Eye in Singapore. Selena is perhaps best-known for creating the Public Domain Web Script Archive (Extropia) and writing several books on Web Programming (Perl, CGI, Java).
Email: selena@extropia.com
|