Introduction to Java : Java Classes part-3

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.

Instantiating and Using an Object

So where are these other objects and how is an Announcer object actually “instantiated”?

To instantiate an object, Java uses the keyword “new”. Consider the following class called Test that instantiates an Announcer and then uses its public API to print the announcement.

We won’t yet focus on the syntax of the Test class. Instead we will just focus on how the Test class utilizes the Announcer class. Notice that the Test class use the new keyword to instantiate an Announcer object and then calls the printAnnouncement() method on the Announcer object using the “dot notation”.

The dot notation is used to access properties or methods in an object. In this case, we have an Announcer object named “a” and we will access the printAnnouncement() method in a.

It is crucial that you specify the object that you want to utilize because it is very possible that there may be more than one Announcer object alive at any given time. Thus, you need to specify which Announcer object you want to access the printAnnouncement method on. Consider the following case in which we apply all of what we just learned about classes. In this example we will create three Announcer objects, two using the default constructor and one using the alternate constructor. We will then use the setAnnouncement API method to change the announcement of the second object and then have each Announcer object print out its announcement using the dot notation