Home » Online Computer Education » Learn Fundamentals of Java Programming “Class Design in Java Programming ” Lesson 18

Learn Fundamentals of Java Programming “Class Design in Java Programming ” Lesson 18

Class Design

We are now ready to create a class in Java. A class in Java begins with the keyword class followed by the name of the class. The body of the class is enclosed within curly braces. The body contains the definitions of the data and method members of the class. The data members are defined by specifying their type. The method members of the class are defined just like the user defined methods we saw earlier.

The method members have access to all the data members of the class and can use them in their body. The data members of a class are like global variables – they can be accessed by all the method members of the class. Figure 1.8a shows how to code the class Book in Java.

Java fundamentals  array 4

Once the class is defined, we can create objects of the class and access its members. To create an object of a class we use the keyword new. We can create objects of the class Book in any Java class file of our package. So we first create a new Java class file, called BookTest.java, in our package. Then in the main method of the BookTest class, we create a Book object (Figure 1.8b). The following statement creates an object named book1 of the class Book.

public static void main(String[] args) {
Book bo ok1 = new Book();
}

Java fundamentals  array 5

Data members and method members of an object are accessed using the dot (.) operator. As soon as you type the name of the object followed by a dot, NetBeans will prompt you with a list of available data and method members that you can access (Figure 1.8c).

Java fundamentals  array 6

Note that the data members and method member that you defined in the Book class are available in the list. Also note that a number of methods that you did not define in the Book class, such as, hashCode(), wait() and so on, are also available in the list. This is because all Java classes inherit from the base class Object. These extra methods are defined in the base class and are therefore available to all Java classes.

You can access all the data members of the object book1 and initialize them as desired (Figure 1.8d). For example, to set the book1’s title, you write:

book1.title = “Game of Thrones “;

Similarly, you can invoke the method member of book1 (Figure 1.8d). For example, in the following statement, the object book1 invokes its method member display().

book1.display();

Java fundamentals  array 7

The display() method as per its method body prints the data members of its invoking object, in this case book1. Figure 1.8e displays the result of executing the BookTest program.

Java fundamentals  array 8

In the BookTest.java program, try to create another object book2, initialize its data members, and then call book2.display(). Run the modified program to verify that now book2’s data members are displayed.

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published.