Classes usually consist of two things:
- Instance Variables
- 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)
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:
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?
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
3. Create two objects box1 and box2 in 'MethodCallingDemo.java' file as shown below:
- Type box1 and type dot '.'
- 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
- First we have written the code for volume( ) method in Box class
- 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( ) )
- 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
- 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)
9 comments:
Hi You are very great, explained full Selenium superb.
My suggestion is bring this in the form of book
You are doing excellent job!!
perfect man
@Prashi @Suman @Sushant - Thank You
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..
@Jenny - Yes we can have same name for variable and a method.
Great work arun,eagerly waiting for your further posts.
Soujanya
Good Explanation arun
Thank you
Post a Comment