Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: how to execute a simple display without a main method

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default how to execute a simple display without a main method

    this example will show you how, i got this code from the net, and i hope someone will support this, i just like to share this tricky way on how to execure something WITHOUT A MAIN method

    public class NoMain {
     
        static {
     
            System.out.println("No Main!");
            System.exit(0);
        }
    }

    i tried to run this in my IDE, but the JVM complies that the class dont have any methods
    try to run it in a command - line.. you will see that it will execute normally like any other class with main


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: how to execute a simple display without a main method

    This is not good practise I have to say, usually you use the static blocks to initialise static variables.

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (January 10th, 2010)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: how to execute a simple display without a main method

    tnx for letting me know that sir json... its been a while ,
    i just got this from a trivia in other posts, i thought this one could be usefull , i was surprised that a user can execute a code without a main method, so i thought this is valid in java practices,,

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: how to execute a simple display without a main method

    Also static blocks are good for loading external DLLs

  6. The Following User Says Thank You to Freaky Chris For This Useful Post:

    chronoz13 (January 13th, 2010)

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to execute a simple display without a main method

    ::GASP:: You said the d-word! DLL's are purely for programming on windows platforms (though, I do believe there is a comparable feature in both linux and osx)

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (January 13th, 2010)

  9. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: how to execute a simple display without a main method

    Quote Originally Posted by helloworld922 View Post
    ::GASP:: You said the d-word! DLL's are purely for programming on windows platforms (though, I do believe there is a comparable feature in both linux and osx)
    I had JNI in mind for Windows lol! I know its not cross- compatable but hey ho, its still true.

    Perhaps I should have said, precompiled external libraries

Similar Threads

  1. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM
  2. adding main method to a code
    By IDK12 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 31st, 2009, 08:52 PM
  3. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM
  4. How to display the contents of my queue?
    By rocafella5007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2009, 11:46 AM