Credits

Sunday, March 1, 2009

More Method Example in Java

We can call a method in one class from another class. Check the sample program below and see the result. Remember that if the class is defined as public, then the class name must be similar to the file name.



[+/-] show/hide



/*Calling a method in another class. This file is saved as SetupSite.java*/

public class SetUpSite{
public static void main(String [] args)
{ StatementofPhilosophy();}

public static void StatementofPhilosophy()
{ System.out.println(" This is an example of invoking a method in another class");}
}

/*THis is the invoking class. This file is saved as TestStatement.java*/

public class TestStatement{
public static void main(String [] args)
{ System.out.println("This is the calling class!");
SetUpSite.StatementofPhilosophy();}}

AddThis