Results 1 to 19 of 19

Thread: java program without main method

  1. #1

    java program without main method

    Hi every one i want to ask you a Question that :-----> Can we made a java program without main method be sure it should not be an applet?


    Second Q. :--> I want to know all the method's of Array declaration in java??


    Third Q. :----> I want to know does main method of java is always a primary thread for java can we change the sequence of main thread from another than please tell me???

    Hey please send a detail's explanation of these Question if possible than send a small program with ans of these Question...


  2. #2
    Junior Member
    Join Date
    May 2007
    Answers
    1

    Re: java program without main method

    i too want the answer for these questions buddy,any body help plz..


  3. #3
    Contributing Member
    Join Date
    Feb 2007
    Answers
    47

    Re: java program without main method

    Hi..

    For the question 1:

    Yes,We can write a Java program which does not have main method at all. This can be done using the static block of the class.

    How this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time, JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.

    class WithoutMain
    {
    static
    {
    System.out.println("This java program ran without the main method");
    System.exit(0);

    }
    }

    For the question 2:
    Some examples:

    Exception ae[] = new Exception[3];
    Object aao[][] = new Exception[2][3];
    int[] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 };
    char ac[] = { 'n', 'o', 't', ' ', 'a', ' ',
    'S', 't', 'r', 'i', 'n', 'g' };
    String[] aas = { "array", "of", "String", };

    For question 3:
    I think,we can change the sequence,but,i am not sure.

    Thanks n' Regards
    Neelima


  4. #4

    Re: java program without main method

    Thanks neelim It's the ideal solution for me....///


  5. #5
    Junior Member
    Join Date
    May 2007
    Answers
    15

    Re: java program without main method

    sorry for the answer given by neelim for the question number 1.

    No java program executes without main method.

    until unless u wil call that class WithoutMain in the main method , static block will not produce output


  6. #6
    Junior Member
    Join Date
    Dec 2006
    Answers
    9

    Re: java program without main method

    sorry for the answer given by neelim for the question number 1.
    No java program executes without main method.
    until unless u wil call that class WithoutMain in the main method , static block will not produce output
    Neelim's answer for Question Number 1 is correct...
    it will generate o/p.
    Memory for the Satic members will be allocated during the compile time itself. and the Interpreter looking for the main method during the execution time if it is not avail , then it throws an java.lang.NoSuchMethodError.if we use System.exit(0) then the programs perfectly run...


  7. #7
    Junior Member
    Join Date
    Oct 2008
    Answers
    2

    Re: java program without main method

    Actually both Neelim & Umeshap are correct to an extent. The code provided by Neelim though correct will not execute by itself as it has no main. So what it needs is another class instantiating it (ie. loading it) as below:

    public class NoMain
    {
    static
    {
    System.out.println("This is an executable class even without a main method");
    System.exit(0);
    }
    }

    public class NoMainTest
    {
    public static void main(String[] args)
    {
    new NoMain();
    }
    }

    So yes we can have a java prg without the main method...

    Hope this helps.


  8. #8
    Junior Member
    Join Date
    Jun 2010
    Answers
    8

    Re: java program without main method

    can anybody tell wats the use of THIS keyword in java??????????


  9. #9
    Expert Member
    Join Date
    May 2009
    Answers
    1,374

    Re: java program without main method

    Quote Originally Posted by raghavmishra View Post
    can anybody tell wats the use of THIS keyword in java??????????

    Hi Raghavmishra,

    The keyword this is useful when you need to refer to instance of the class from its method. The keyword helps u to avoid name conflicts.

    Eg:


    public class GeekTalk

    {
    final static int MAXX=640, MAXY=480;
    int x, y;


    GeekTalk(int x, int y)

    {
    this.x = x;
    this.y = y;

    }

    GeekTalk()

    {
    this(MAXX/2, MAXY/2);
    }

    public String toString()

    {
    return "[" + x + "," + y + "]";
    }


    public static void main(String[] av)

    {
    System.out.println(new GeekTalk(300,100));
    System.out.println(new GeekTalk());
    }
    }

    Thanks,
    Riju's.

    Real Inspirational Journey........Unanimously & Sincerely.

  10. #10
    Junior Member
    Join Date
    Jun 2010
    Answers
    8

    Re: java program without main method

    this code is very tough to understand riju i m beginer so can u give some simple code....for this keyword


  11. #11
    Junior Member
    Join Date
    Jun 2010
    Answers
    1

    Re: java program without main method

    Neelam's answer is correct,recently i heard about this concept

    yaa we can execute an java program without main method..




    Thanks
    Sagar Chugh


  12. #12
    Junior Member
    Join Date
    Jun 2010
    Answers
    7

    Re: java program without main method

    Ur first answer:
    Yes,u can make a java program without main().You can also compile it and a .class file will be generated after compilation.But you cannot execute it,because when you try to execute this .class file, you will get a runtime error called java.lang.NoSuchMethodError:main.

    Ur second answer:
    The various ways in which we can declare an array are:
    First way:
    Here,only an array is created but elements in the array get default values.
    int [] arr=new int [5];
    Second way:
    int arr[]=new int[10];
    Third way:
    Here in addition to declaring the array,you are also giving the values to the elements in the array.Here,you dont need to specofy the size of array.Depending on the number of entries in the curly braces i.e{},the size is automatically assigned to the array.So,here the size of array becomes 4.
    int arr[]={1,2,6,9};
    Fourth way:
    int arr[]=new int [] {1,2,3};

    Ur third answer:
    main() will always be the first thread to be executed.When the java run time executes the program,the first thread to be executed is main().From inside main(),other threads are created and then those threads are started.Each thread has got a priority.In java, priority of main() is 5.You can set the priority of other threads greater than the priority of main().So,if you set the other thread at a greater priority,that thread will be given more importance by the JVM.But,main() must be the first thread executed by the JVM and other threads can be started only from within main. Also main() is the last thread to be executed before the program ends.


  13. #13
    Junior Member
    Join Date
    Jul 2010
    Answers
    1

    Re: java program without main method

    Quote Originally Posted by raghavmishra View Post
    can anybody tell wats the use of THIS keyword in java??????????
    It returns the reference to the current object


  14. #14
    Junior Member
    Join Date
    Jun 2010
    Answers
    7

    Re: java program without main method

    Quote Originally Posted by raghavmishra View Post
    can anybody tell wats the use of THIS keyword in java??????????
    The keyword this always refers to the current object i.e the object on whose reference the current action is being executed.
    If both the local variable and the instance variable have the the same name inside a method,then the local variable always shadows the instance variable.So,how will you access the instance variable inside the method if the locale variable also has the same name as instance variable?In those cases we can use 'this' keyword.
    We can understand it with an example:
    1.class Example
    2.{
    3. private int a;
    4. public void setA(int a)
    5. {
    6. a=a;
    7. }
    8.}
    9.class UseExample
    10.{
    11. public static void main(String args[])
    12. {
    13. Example e=new Example();
    14. e.setA(5);
    15. }
    16.}
    I have included the line number in previous example to make it more clear.In this program,we have created an object on line 13.We want to set the value of this object's instance variable 'a' to value say,5.We use the setA() method defined on line 4.But in setA(),local variable(i.e parameter) also has name as 'a'.So,the local variable inside the method shadows(hides) the instance variable 'a' and in this example we are not able to set the value of instance variable 'a' as 5 because it has been shadowed by local variable 'a'.In the method setA() all 'a' means the loval variable 'a'.
    We can solve this problem using the keyword 'this'.If we change the code at line 6 to:
    6. this.a=a;
    then the value of instance variable 'a' is set equal to the value of local variable 'a' i.e 5.This happens because we wrote 'this.a=a'.Its a way of saying the compiler that "set the current object's 'a' to local variable 'a'.But local variable 'a' has the value 5.So,the instance variable 'a' is set to 5 " .Here the current object is the object which was created on line 13.Since'we invoked the method setA() using the reference of the object created on line 13 so that object's instance variable 'a' is referred when we write this.a on line 6.So,'this' refers to the object on which the method is invoked.
    I hope you are clear.
    'this' has some other uses also like, this() means the current object's constructor.But you can understand those things once you understand what the keyword 'this' means.Remember,this always refers to the current object i.e the object on which the work is being currently done i.e the object on which the method is invoked.

    Last edited by akshar.raaj; 07-01-2010 at 11:32 AM.

  15. #15
    Junior Member
    Join Date
    Sep 2011
    Answers
    1

    Thumbs up Re: Java program without main method

    class WithoutMain
    {
    static
    {
    System.out.println("This is a Program without a main method.");
    System.exit(0);
    }
    }


  16. #16
    Junior Member
    Join Date
    Oct 2011
    Answers
    1

    Re: java program without main method

    I ran this program. Wow amazing. It ran successfully. But I just want to know how it works internally?


  17. #17
    Junior Member
    Join Date
    Oct 2011
    Answers
    2

    Re: Java program without main method

    The program wont run. Shows the following error.
    Error: Could not find or load main class WithoutMain


  18. #18
    Junior Member
    Join Date
    Nov 2011
    Answers
    4

    Re: java program without main method

    There is no magic behind this. static block gets called when ClassLoader tries to load the class.after loading the class only jvm calls the main method. so no wonder it executes the statements in the static block with out main method.


  19. #19
    Junior Member
    Join Date
    Apr 2013
    Answers
    1

    Re: java program without main method

    Hi neelim ,
    The program you provided will compile successfully,but will not execute due to main not found exception.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact