Thursday, October 9, 2008

Java - Calling Functions

This is the program that is executed:
public class callingClass
{
public static void main(String[] args)
{
calledClass calledClassInstance = new calledClass();

calledClassInstance.function1();
calledClassInstance.staticFunction2(); //call static function with
calledClass.staticFunction2(); //instance name or class name
}
}

This is called:
public class calledClass
{
long n1 = 10, n2 = 20, total, product;

public void function1()
{
total = n1+n2;
System.out.printf("\nTOTAL = %d\n",total);
}
public static void staticFunction2()
{
product = n1*n2;
System.out.printf("\nPRODUCT = %d\n",product);
}
}

No comments:

Post a Comment