• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running this code  RSS feed

 
anandashankar sen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written this code, compiled and executed in NOTEPAD from command prompt.
class A{

public static void main(String args[]){
System.out.println("In A");
}
}

class B extends A{

public static void main(String args[]){
System.out.println("In B");
}
}

class C{
public static void main(String args[]){
System.out.println("In C");
}
}
The file name is C.java so the output is In C. When I am running the same code in JCreator version 3.50, the out put is coming depending the top most class. If it is A, as in this case, the output is coming "In A". When I am keeping C as the top most class, the output is coming as "In C". Why is this happening?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike (most) native languages (like C), Java does not have a single entry point for an application. In fact, any class with a "public static void main(String[] args)" method is an entry point.

On the command line, you explicitly tell the JVM to execute the main method of class C (by calling "java C"). If you would call "java A" instead then it would execute the main method of class A.

Eclipse, and probably also JCreator, will try to execute the main method of the currently selected class. In a similar fashion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!