• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can a program without "main" method show output?

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another ugly example, since we're on the topic:
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Dirk,

that was very nice of you . I have all my concepts clear now.
 
Crusading Chameleon likes the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic