Home | | Sitemap ||Page number :8

Understanding Classes and Functions

Introduction In the first chapter, you studied about OOP, classes, functions and their use in Java language. As it was just a start you were introduced with certain basic concepts. The most powerful features were left untouched, e.g. - You had only seen the use of main method but are yet to discover the concept of using multiple methods in a class other then the “main”. Now you will explore them in depth. Before going to deep let us revise the things and start from basics.

A general class declaration with methods---

class name1 { //public variable declaration

void methodname() { //body of method… //Anything } }

Let us apply this concept and take an example of real world class.

class learn { private int x,y,z; public void input() { x=10; y=15; } public void sum() { z=x+y; } public void print_it() { System.out.println(“Answer is =” +z); } } This is a class ‘learn’ with three methods, i.e., input, sum and print. The three functions are designed to do a specific task. This you can easily figure out by seeing the code. The input () method initializes the two values, sum () adds them and finally print_it() shows the answer.

Will the class compile and execute?

Try it on your own, you will find that the class compiles in the DOS environment but fails to execute. If you will try it in the BlueJ environment, it will compile as well as execute successfully (This clearly shows the advantage of using BlueJ).

The class did not execute because of the absence of the “main” method. You already know that the main method is the first one to execute. As soon as we run a java program the control is transferred to “main”. So we will now add a main method to the “learn.java” program.

class learn { private int x,y,z; public void input() { x=10; y=15; } public void sum() { z=x+y; } public void print_it() { System.out.println(“Answer is =” +z); }

public static void main(String args[]) { learn object=new learn(); object.input(); object.sum(); object.print_it(); } }

Try to compile the program and voila!!! it runs successfully. Now let us see what to these additional lines mean. We have added the main method to our program. Look at the following code :

learn object=new learn(); object.input(); object.sum(); object.print_it();

In the first line we created an object (A much talked subject in the first chapter!!). An object can be simply created by typing---

learn object; The object thus created will be null and so we need to instantiate it. The new operator allocates memory for the object and instantiate it . Constructors are also called at this point (You will read about constructors in the next chapter). The three methods are called by using the dot operator. When we call a method the code inside its block is executed.

The dot operator

The dot operator is used to call methods or access them. It has other use too, which we will discuss in this chapter later.

Creating “main” in a separate class

You can even create the main method in a separate class, but during compilation you need to make sure that you compile the class with the “main” method. The program will thus be like this….

class learn { private int x,y,z; public void input() { x=10; y=15; } public void sum() { z=x+y; } public void print_it() { System.out.println(“Answer is =” +z); } }

class apply { public static void main(String args[]) { learn object=new learn(); object.input();

object.sum(); object.print_it(); } }

Another use of dot operator Read the next program carefully and try to figure out its working.

class dot1 { int x,y,z; public void sum(){ z=x+y; } public void show(){ System.out.print(“The Answer is “+z); } }

class apply1 { public static void main(String args[]){ dot1 object=new dot1(); dot1 object2=new dot1(); object.x=10; object y=15; object2.x=5; object2.y=10; object.sum(); object.show(); object2.sum(); object2.show(); }}

The output of the program is—

x

Before you understand this concept properly, a little theory is needed to be understood.