Results 1 to 6 of 6

Thread: Write Code To Execute Before Main Method

  1. #1
    Join Date
    Nov 2009
    Posts
    652

    Write Code To Execute Before Main Method

    Hello, I am learning Java programming language. And completed the course also. But, few days ago one of my friend told me to write the code which will execute before the main method and I was unable to do that. Can anyone tell me how to write a code which will execute before main method. Can anyone tell me how to write this particular code. I am waiting for your reply.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Write Code To Execute Before Main Method

    I don't think that this would be possible. As when you write any type of code first execution is done with the Main Method and then only you will able to get to execute all other line of code. So, it would be impossible to execute any type of code of line from the program which will able to execute before the Main method.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Write Code To Execute Before Main Method

    It may be possible as below done. Here your main method is defined into the Interface, which mean your interface is executed first and then your main method will execute.
    Code:
    public interface InterfaceMain
    {
    public class Main
    {
    public static void main(String [] args){
    System.out.println("Main inside interface");
    }
    }
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Write Code To Execute Before Main Method

    Yes, you can do that. If you don't provide any type of main method then also you can provide any message as below:
    Code:
    public class Trial
    {
    static 
    {       
            System.out.println("Hello");
            System.exit(0);
    }
    }

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Write Code To Execute Before Main Method

    I have searched for this on internet, but not find any type of solution for your problem. I think you need to make use of different books for getting solution for this purpose. I don't think that you will able to get any type of this information directly. You need to read books which will provide your tricks in java programming. For that you can make use of the Tricks of the Java Programming Gurus book also.

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Write Code To Execute Before Main Method

    It is quiet simple to execute code before Main Method, you just need to use the static keyword in your program as below:
    Code:
          static
          {
          //Provide your source code here
          }

Similar Threads

  1. Explain main method in java
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 10-03-2010, 01:18 PM
  2. Why main method is static in java?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:20 PM
  3. Unable to call class into main method in c# code.
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 07:02 PM
  4. How to write code for pause or stop method in HTML
    By TechPredator in forum Software Development
    Replies: 3
    Last Post: 10-08-2009, 02:39 PM
  5. What if we declare the main method as private?
    By Baran in forum Software Development
    Replies: 4
    Last Post: 25-02-2009, 07:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,548,507.53184 seconds with 16 queries