• Post Reply Bookmark Topic Watch Topic
  • New Topic

I have some doubts.  RSS feed

 
Biju Mahadevan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends

1. Can a program execute with a main function. I mean to say a java program without main function.

2. Without creating object, is it anyway we can call the method in the main function.[Note the method is not declared static].
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a beginner question.

[Biju Mahadevan:]   I mean to say a java program without main function.

1: I understand that there are tools that can do this for testing. I have not used them, I just put a main in the class.

2: Not if the method is not static.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that class still has to be loaded before that code is run.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ofcourse the class has to be loaded to run it. You can compile and run my example just like any other Java program and it works - did you try it out?

By the way, this is a dirty trick - don't do things like this in real Java programs please.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I get it. Because of the System.exit(0), it closes the JVM before it gets the chance to throw the error about the missing main method.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!