Introduction to Java Methods

This tutorial will take you step by step in understanding the basics of a method , how to define it and how to use it. The best way to learn is to compile and run these programs yourself (copy, paste, compile and run !). Comments such as /* this is a comment */ or // this is another comment are inserted to explain what does the line of code do. The programs are kept simple for the purpose of concentrating on the main idea in question.

Example 1: method that calculates and displays a volume

 

class Room1{ 

   double length;
   double width;
   double height;


// this defines a method called volume that calculates and displays the volume 
// of a given object whose instance variables are length, width and height.
// IMPORTANT: the instance variables length, height and width are 
// referred to directly inside the method volume .
 
   void volume(){
   double vol = length*width*height;
   System.out.println("volume of this room is =  "+vol);
   }

   public static void main(String t[]){
     
      Room1 myroom1 = new Room1(); // creates object myroom1 
      Room1 myroom2 = new Room1(); // creates object myroom2

      myroom1.length= 10.0; // assigns values to instance 
      myroom1.width = 8.0;  // variables of myroom1
      myroom1.height = 5.5;

      myroom2.length= 9.0;  // assigns values to instance 
      myroom2.width = 7.0;  // variables of myroom2
      myroom2.height = 4.5;

// this is how you you invoke the volume method on myroom1
      myroom1.volume(); // Note the use of the dot (.) operator when invoking 
                        // a method.

// this is how you you invoke the volume method on myroom2
      myroom2.volume();
   }
}
      

The above program defines a template for the class Room1, with instance variables length, height and width and a method called volume that calculates the volume .

IMPORTANT: the instance variables length, height and width are referred to directly inside the method volume. This is because when we invoke a method we invoke it relative to an object : myroom1.volume().

Example 2: a method that calculates and returns a volume

  
class Room1{ 

   double length;
   double width;
   double height;

// this defines a method called volume that calculates the volume 
// of a given object and returns it.
   
   double volume(){ // the returned volume is of type double
   return length*width*height; // returns the volume.
   }

   public static void main(String t[]){
     
      Room1 myroom1 = new Room1(); // creates object myroom1 
      Room1 myroom2 = new Room1(); // creates object myroom2

      myroom1.length= 10.0; // assigns values to instance 
      myroom1.width = 8.0;  // variables of myroom1
      myroom1.height = 5.5;

      myroom2.length= 9.0;  // assigns values to instance 
      myroom2.width = 7.0;  // variables of myroom2
      myroom2.height = 4.5;

// this is how you you invoke the volume method on myroom1
      double vol1 = myroom1.volume(); 
// displays vol1
      System.out.println("volume of myroom1 = "+vol1);

// this is how you you invoke the volume method on myroom2
      double vol2 = myroom2.volume();
// displays vol2
      System.out.println("volume of myroom2 = "+vol2);
   }
}

In example 1 the keyword void was used that is because the method volume there did not return anything.

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.