aspose file tools*
The moose likes Beginning Java and the fly likes can a program without Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can a program without "main" method show output?" Watch "can a program without "main" method show output?" New topic
Author

can a program without "main" method show output?

Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
Hi,
I came across this question.

Can a java program without a main method show some output?

the answer was 'yes'. can somebody explain this to me.

also, I have seen many mock exams on the net, some are easy some are difficult. Can someone please provide me a link to some mock exams which are of the "SAME" level of difficulty (or ease) as the original exam. I am prepared for the exam, however, I want to see how the questions there look like, so that i can perform really good there.

Bye.


Impossible is I M Possible
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Can a java program without a main method show some output?

the answer was 'yes'. can somebody explain this to me.


To be clear, I wouldn't consider this a program, because it doesn't have a proper main method. This example takes advantage of a static initialization block. Such blocks are run whenever a class is loaded into memory. This class is loaded into memory with the command java Foo. Perhaps this is what your questioner was getting at.

It also occurs to me that Applets, Servlets, and Midlets don't have main methods.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Can someone please provide me a link to some mock exams which are of the "SAME" level of difficulty (or ease) as the original exam.

Take a look at the list and accompanying descriptions of mock exams at http://www.javaranch.com/mock.jsp
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
Thanks dirk,

My doubt is clear now, I also figured out that if I have the main method in a "super" class and have no main method in the subclass, even then the condition of the question would have been fulfilled. Thanks for giving me the link for the mock exams.

wish me luck guys.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Another ugly example, since we're on the topic:
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
maybe nitpicking, but a class without a main method is no program.
At most it's part of a program but it will never be a program itself.


42
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Hey Guys,

I too have come across it, but not so clear with that. What's happening when such a piece of code is loaded?

Could you please expand on the execution of the program?

Cheers,
Swamy
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jeroen Wenting:
maybe nitpicking, but a class without a main method is no program.
At most it's part of a program but it will never be a program itself.

I would argue that an Applet is a program even though it doesn't contain main(). I think this is a better example of a "Java program" without main() that can still produce output. MIDlets are another example, although they are part of J2ME. Perhaps Servlets can be consider in the same light, although I'm not familiar at all with J2EE.

So the question I have is what exactly do you mean by a "Java program"?

-Layne
[ September 21, 2004: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
Hi swamy,

I am the one who posted this topic here and since I am preparing for the certification, I tried to get to the roots of this problem. As far as I think, when you type the command to execute this program, the compiler first comes accross the static initializer blocks, and then starts executing the commands in these initializer blocks (until now the compiler didnt need the main method ), after doing the work in the static initializer it searches for the main method, when it cannot find the main method it throws an Exception and the program stops abruptly. To stop this to do that, one could place

and thus you have a program without a main but still showing u some output, (now if you can call it a program or not, is a different thing all together).

Guys, I dont have programming experience and my explaination may be wrong here, however i would really appreciate if one of you could comment on that and if there is something wrong there please point it out, so that my doubts are clear.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I think you get the idea.

When the JRE loads the class, it first runs any static initializers, the rest is as you said.
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
Thank you Dirk,

that was very nice of you . I have all my concepts clear now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can a program without "main" method show output?