Login    New User    Forgot Password    Help      

Returning Value

Java Tutorials

The values that are passed to a method are called actual arguments in the Java specification. However, it is very common for them to be called just argumentsactual parameters, or just plain parameters. These terms are so used interchangeably so often that even the Java specification isn't entirely consistent. For a value which is passed in a call I'll try to stick to actual argument or just argument, which are generally regarded as the "best" terms.

Example

class abc
{
int a;
int b;
        void getdata(int x,int y)
        {
        a=x;
        b=y;
        }
        void showdata()
        {
        System.out.println("Value of a is " +a);
        System.out.println("Value of b is " +b);
       
        }
       
}

class clas3
{
public static void main(String args[])
{
// Create ob1 object of class abc

abc ob1=new abc();
ob1.getdata(10,20);

ob1.showdata();

}

}

Output

e:\java\java clas3

Value of a is 10

Value of b is 20

Methods Agruments and Returning values

A method may return a value. In the example above, showInputDialog returns a String and parseDoublereturns a double value. These method calls can be used anywhere in an expression where a String or double value is required. Here they simply provide the value for the right side of an assignment.

void. If a method has a "side effect", but doesn't produce a value, it is called a void method. TheshowMessageDialog method shows something to the user, but doesn't return a value, and is a void method.

When a method is defined, you need to specify the keyword void if it doesn't return a value.

Example


class abc
{
int a;
int b;
int sum;
void getdata(int x,int y)
{
a=x;
b=y;
}
// here decleration of datatype is mendatiory
int showdata()
{
sum=a+b;
return sum;
}
}

class clas4
{
public static void main(String args[])
{
// Create ob1 object of class abc
int sum;
abc ob1=new abc();
ob1.getdata(10,20);

sum=ob1.showdata();
System.out.println("Sum is "+sum);
}
}

Output

e:\java\java clas4

Sum is 30

Java Tutorials