This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Running this code  RSS feed

 
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?
 
Sheriff
Posts: 21288
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!