Tuesday, November 18, 2008

Call External method in java

In this lession you will learn to create a method and call it inside from the main method. Following is the sample code for create a simple method to print a message to the console and call that method inside from the main method.

***********************************************************
public class FunctionCall {
public static void function1(){
System.out.println("Message from outside method");
}

public static void main(String[] args) {
System.out.println("Inside the main");
System.out.println("About to call function1");
function1();
}

}
***********************************************************

You will see the output with two messages printed on the console. First message is the message specified in the main method ("Inside the main") and next you will get the message specified in the method called "messageFunction".
Output will be like

----------------------------------------------------------
Inside the main
About to call function1
Message from outside method
----------------------------------------------------------

No comments: