Programming Tutorials

Methods in Java

By: aathishankaran in Java Tutorials on 2007-03-06  

Classes usually consist of two things:- Instance variable and Methods. The topic of methods is a large one because java gives them so much power and flexibility. However, there are some fundamentals that you need to learn now so that you can begin to add methods to your classes.

This is the general form of a method:

Type name( parameter-list) {

    //body of method

}

Here, type specifies the type of data returned by the method. This can be any valid type, including class types that you create. If the method does not return a value, its return type must be void. The name of the method is specified by name. This can be any legal identifier other than those already used by other items within the current scope. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the method when it is called. If the method has no parameters, then the parameter list will be empty.

Methods that have a return type other than void return a value to the calling routine using the following form of the return statement:

return value;

Here, value is the value returned.

Adding a method to a class

Although it is perfectly fine to create a class that contains only data, it rarely happens. Most of the time you will use methods to access the instance variables defined by the class. In fact, methods define the interface to most classes. This allows the class implementor to hide the specific layout of internal data structures behind cleaner method abstractions. In addition to defining methods that provide access to data, you can also define methods that are used internally by the class itself.

Let us begin by adding a method to the test class. It may have occurred to you while looking at the preceding programs that the computation of a summation was something that was best handled by the test class rather than the testdemo class. After all, since the volume of a box is dependent upon the values, it makes sense to have the test class compute it. To do this, you must add a method to test class, as shown here:

class Test {
     double item1;
     double item2;
     double item3;
     void value() {
          System.out.println("value is ");
          System.out.println( item1 + item2 + item3);
     }
}

class Testdemo() {

     public static void main (String args[]) {
          Test test1 = new test(); 
          Test test2 = new test();
		  
          test1.item1 = 10;
          test1.item2 = 20;
          test1.item3 = 15;

          test2.item1 = 3;
          test2.item2 = 6;
          test2.item3 = 9;

          test1.value();
          test2.value();
     }
}

This program generates the following output,

value is 45.0
value is 18.0

Look closely at the following two lines of code:

test1.value();
test2.value();

The first line here invokes the value () method on test1. That is, it calls value() relative to the test1 object, using the object's name followed by the dot operator. Thus, the call to test1.value() displays the value of the members defined by test1 and the call to test2.value() displays the value of the members defined by test2. Each time value() is invoked, it displays the volume for the specified data member.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)