Google+

71. Methods







Classes usually consist of two things:
  1. Instance Variables
  2. Methods


Method Syntax:

type name(parameter list)
{
   //body of method
}

type - The type of data returned by method
  • If the type is specified as void, the method don't return anything (i.e. return value; is not used)
  • If the type is specified other than void say int,double etc, it means the method will return int/double etc (i.e. return value; is used to return the int/double etc values)
name - The name of the method

parameters - Variable that receive the values of the arguments passed to method when it is called
  • Parameter list can be empty



Example of a method which don't return anything (As discussed above, the method which don't return anything use void type)

void volume( )
{
     System.out.println("Volume is "+(width*height*depth));
}


Example - Calling a method in Box class from 'MethodCallingDemo' class

1. Launch Eclipse IDE, ensure that 'Third Project' java project is displayed and copy the method shown in the below screenshot  into the Box class as shown below and save the Box.java file



2. Create a new class file called 'MethodCallingDemo' under third_package of Third Project as shown below:



3. Create two objects box1 and box2 in 'MethodCallingDemo.java' file as shown below:



4. Assign values to the box1's  instance variables as shown below:



5. Assign values to the box2's instance variables as shown below:



6. Calling volume( ) method in class Box for printing the volume of  box1 as shown below:
  1. Type box1 and type dot '.
  2. Eclipse IDE will list all the instance variables and methods of Box class and ensure that volume( ) method of Box class is displayed as shown below


     3. Select the volume( ) method, ensure that it is added to your code and end it with ; as shown below



7. In the similar way call the volume( ) method of Box class to calculate the volume of box2 as shown below:


8. Save and Run the 'MethodCallingDemo.java' using Java Application as shown below:


9. Observe that the following output is displayed as shown below:



What happened? How did we get the output?
  1. First we have written the code for volume( ) method in Box class
  2. Second we have accessed the volume( ) method from the 'MethodCallingDemo' class by creating the objects for Box class and by calling the volume( ) method using the following syntax object.method( );  (i..e. box1.volume( ) ) 
  3. volume( ) method in Box class calculated and printed the volume of box1 by using the values assigned to box1.height, box1.width and box1.depth variables in 'MethodCallingDemo' class 
  4. Similarly the volume of the box2 is also calculated and printed in the same manner
Download this project:

Click here to download the Project containing 'MethodCallingDemo.java' file. (You can import the downloaded file into Eclipse IDE in your machine)





Please comment below to feedback or ask questions.

How does a method return a value will be explained with an example in the next post



9 comments:

Prashi said...

Hi You are very great, explained full Selenium superb.

My suggestion is bring this in the form of book

Suman said...

You are doing excellent job!!

Unknown said...

perfect man

Arun Motoori said...

@Prashi @Suman @Sushant - Thank You

Unknown said...

I have one more question:
Can variable and function have same name???.. as i made a class where i have kept same name for variable and same name for method but while running the class it didnt give me an error..

Arun Motoori said...

@Jenny - Yes we can have same name for variable and a method.

Anonymous said...

Great work arun,eagerly waiting for your further posts.

Soujanya

Anonymous said...

Good Explanation arun

Arun Motoori said...

Thank you