• 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

How to print message without main and static

 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to print a message to console without using main()
function?(do not use even static blocks also.)
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that class is the only class in your application and if it does not have the public static void main() method the program will not run. JVM throws a run time exception since it cannot find the main(). So what is it you are trying to do?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

I presume you want that as a party trick (it isn't suitable for real-life use). I know how you can start an app with a static initialiser and no main() method, but I can't see it being possible without both.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is nearly equivalent to using a static initializer - but it's not a static initializer.

I guess another way to do this might be by writing a Servlet, JSP, Applet, or other object that relies of a framework of some sort to invoke it. Except that in the case of Servlets and JSPs at least, there really is a main() method for the server. It's just not in code that you write - it's kind of hidden from view. For an Applet, I'm not sure. I suspect there's probably a main() method being used there too, but I don't know where it is offhand. I agree with Campbell - this doesn't seem like a very useful question for real-life use.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This recent discussion added a couple more twists.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the intent of these types of parlor tricks... uh, I mean, questions is to get you to think about what happens when a class is initialized. In particular, understand that static blocks are executed and static fields are initialized before main is invoked. (Note: You can avoid the NoSuchMethodError by inserting a System.exit(0) at the appropriate place.)

But once this lightbulb has gone on, you should also recognize this for what it is (and isn't).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic