this is the difference between static and non-static

Saturday, January 31, 2009

Hello world

Exercise

Create a java file named Hello.java and define two classes named HelloWorld and AnotherHelloWorld containing the main method for defining an application, the main method should have statements to print “Hello world!” and “Another Hello world!” respectively. Compile the java file and check the output of compilation(what files are generated). Now execute the application.

The content of the file can be as follows:
Content of file Hello.java follows:


class HelloWorld {
public static void main(String[] args} {
System.out.println(“Hello world!”);
}
}

class AnotherHelloWorld {
public static void main(String[] args} {
System.out.println(“Another Hello world!”);
}
}


The above code compiles without errors.
The output of compilation is two class files named, HelloWorld.class and AnotherHelloWorld.class.
Both the applications run and produce the output.

Lessons learned:
1.A single java file may contain any number of class definitions,
2.Each class defined in java is compiled into a separate class file whose name matches the class name.
3.Each class definition has its own separate class file.
4.The main method is used to create an application in java, and an application is embedded in a class definition.
5.Applications are executed by using the class name which contains the application(main method).

2 comments:

krishna said...

After compile Hello.java 2 class file generated HelloWorld.class and AnotherHelloWorld.class but when i try to run HelloWorld using java command then it generates Error like class not found

Pravin Jain said...

@krishna.
try removing any classpath setting using the following commnand
set CLASSPATH=
and then use the java command to run the application.