In the last lession you learned how to create a method and call it from main method. This time you will learn how to create a method with parameter and call it from main method.
**********************************************************
public class FunctionCall {
public static void function1(){
System.out.println("Message from outside method");
}
public static void function2(int param){
System.out.println("Inside a function with parameter"+param);
}
public static void main(String[] args) {
System.out.println("Inside the main");
System.out.println("About to call function1");
function1();
System.out.println("About to call function2");
function2(2008);
}
}
********************************************************
You will get output like below, after execute the above programe
---------------------------------------
Inside the main
About to call function1
Message from outside method
About to call function2
Inside a function with parameter2008
---------------------------------------
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
----------------------------------------------------------
***********************************************************
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
----------------------------------------------------------
Sunday, November 16, 2008
Getting Start
Welcome to javaFromBegining
Before start to learn Java, You should setup you'r computer so that a Java programme can be executed. For that, you should instal JDK on you'r machine. JDK can be downloaded from www.sun.java.com.
After you instal JDK, now it's time to setup the class path. Class path variable should set up to "BIN" derectory of java installation derectory. If you allowed to instal java in to it's default derectory, useually it instals in to "C:\Program Files" derectory. If so, the class path variable should set up to "C:\Program Files\Java\jdk1.5.0\bin" Location.

Now you are ready to start programming.
Let's start with a simple programme which is print a message to the console.
Code for a simple application can be done by using notepad or any text editor. But when you are moving to develop large application, it is hard to code with notepad. There are many tools to use for developments. Following tools are 2 from them
I used Eclips for developments.
Following is the code sample for print a message to the console.
*****************************************************
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World.");
}
}
*****************************************************
Now the source code should compile before it run. To do that, open the command prompt and move to the java source code derectory. After that type command "javac Helloworld.java" to compile.
Ex:-
C:Temp\> javac Helloworld.java
if there are no errors, You can run the programme by typing following code on command prompt.
"java Helloworld" and then it prints the Message you defined to print(Hello World)
Ex:-
C:\>java Helloworld
C:\>Hello World
Before start to learn Java, You should setup you'r computer so that a Java programme can be executed. For that, you should instal JDK on you'r machine. JDK can be downloaded from www.sun.java.com.
After you instal JDK, now it's time to setup the class path. Class path variable should set up to "BIN" derectory of java installation derectory. If you allowed to instal java in to it's default derectory, useually it instals in to "C:\Program Files" derectory. If so, the class path variable should set up to "C:\Program Files\Java\jdk1.5.0\bin" Location.
Now you are ready to start programming.
Let's start with a simple programme which is print a message to the console.
Code for a simple application can be done by using notepad or any text editor. But when you are moving to develop large application, it is hard to code with notepad. There are many tools to use for developments. Following tools are 2 from them
- Netbeans
- Eclips
I used Eclips for developments.
Following is the code sample for print a message to the console.
*****************************************************
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World.");
}
}
*****************************************************
Now the source code should compile before it run. To do that, open the command prompt and move to the java source code derectory. After that type command "javac Helloworld.java" to compile.
Ex:-
C:Temp\> javac Helloworld.java
if there are no errors, You can run the programme by typing following code on command prompt.
"java Helloworld" and then it prints the Message you defined to print(Hello World)
Ex:-
C:\>java Helloworld
C:\>Hello World
Subscribe to:
Comments (Atom)