Wednesday, 8 July 2015

Using Methods For Returning A Value In Java

Before reading this tutorial you should have enough knowledge regarding methods of java so first see our tutorial Tutorial On simple method in java.

RULES OF RETURNING VALUE FROM METHOD:

For returning a value from method a following conditions have to be fulfilled 
  • Method Should have a return statement.
  • Return type should be declare in a method as I have done in this program double volt() its mean that method with void declaration can not return any value.
  • Return data type value should match the declared data type such as double is declared as a return type in this program so we can not return an int type data from method..

CODING:

class current
{
double current;
double resistance;
double volt() 
{
return current*resistance;
}
}

class voltage
{
public static void main(String args[])
{
//Creating Object Of current class
current cur=new current();
//assiging value of current and resistance in instance variable
cur.current=30;
cur.resistance=100;
System.out.println("Voltage is = "+cur.volt());
}
}  

OUTPUT:



No comments:

Post a Comment