public class test{public static void main(String args[]){System.out.println("Hello");}}
Save the above file as test.java
Compile the above program: javac test.java
run the program: java test
Output would be: Hello
In Java we write every thing in class. A class should contain a main method which is static and should return void. String args[] is used to take command line arguments.
so far we learned basic program we into some more depth
we will learn different ways of calling a method
public class test{public static void main(String args[]){int i;test1 t=new test1(); // creating a instance of test1i=t.normal(); // calling a method in class test1System.out.println(i);}}class test1 // this class should not be public{public int normal(){try{return 10;}finally{return 20;}}}
Save the above file as test.java
Compile the above program: javac test.java
run the program: java test
Output would be: 20
Above program we wrote two classes, and accessing a method in one class from another class.
public class test{public static int normal(){ // this method should be statictry{return 10;}finally{return 20;}}public static void main(String args[]){int i;test1 t=new test1(); // creating a instance of test1i=t.normal(); // calling a method in class test1System.out.println(i);}}
Save the above file as test.java
Compile the above program: javac test.java
run the program: java test
Output would be: 20
In the above above program we wrote two methods in one class and both method are static. We made normal method static because we want to access it from main method.
1 comment:
change the spelling of programme - Program
Post a Comment