Main function theory question

Hello,
Java is said to be pure object oriented.
In order to make an executable program, one must provide a main function in the class of execution.
This behavior is not pure object oriented.
why couldnt a program start running by executing the constractor of the main class?
if you think this question is posted in the wrong forum, plz notify me where should i post it.

The main method also exists for practical reasons. How else would the JVM pass command-line parameters to your program? Either you require that a class has a static main, or you require that it has a constructor that accepts an array of Strings. But what if your class requires a constructor with an array of Strings as it's only parameter? How does Java know that this isn't an entry point to the program? The static main therefore acts as a clear entry point, whereas constructors do not.
I've never used Eiffel, but I have a tough time imagining how you could start a program from anything but a static context. Even if you started from a Constructor, it just means that the RT is creating a static context for you under the hood from which it is calling your constructor. This way, you at least have control over the context you are using.

Similar Messages

  • Functionality of main function

    Hello there:
    what's the functionality of main function in a program?
    This is a question asked in an interview. The answer on my mind is, main function is the entry for a program to start running.
    Any more addtional and completed explainations?
    Thanks,
    sway

    Any more addtional and completed explainations?The correct reply is to stand up, politely say that enought is enought, and then proudly leave the room without looking back. -:)

  • Static variable error in main function

    Dont know why it would be doing this. It is bascially telling me that every class I call in my main function needs to be static which is crazy.
    import javax.swing.*;
    import java.awt.*;
    class Frame extends JFrame
         public Frame()
              setTitle("My Empty Frame");
              setSize(300,200);
              setLocation(10,200);
              this.show();
    public class Main extends JFrame
         public JFrame frame;
         public Main()
              frame = new JFrame();
         public static void main(String[] args)
              System.out.println("CS348 Project 1");
              frame.setVisible(true); <--------------------ERROR
    }Thanks for the help.

    The problem (basically), is that you could do the following if you felt like it:
    import javax.swing.*;
    import java.awt.*;
    class Frame extends JFrame
         public Frame()
              setTitle("My Empty Frame");
              setSize(300,200);
              setLocation(10,200);
              this.show();
    public class Main extends JFrame
         public JFrame frame;
         public Main()
              frame = new JFrame();
         public static void main(String[] args)
              Main main1 = new Main();
              Main main2 = new Main();
              Main main3 = new Main();
              //what frame is this talking about??
              frame.setVisible(true); <--------------------ERROR
    }What you would want instead is probably something like this:
    Main main1 = new Main();
    main1.getFrame().setVisible(true);This leads to a few other questions: why are you extending JFrame if you have a JFrame variable in the class? Does that class have a JFrame, or is it a JFrame? You should almost definitely go with the "have" answer.

  • Main function in c programming

    I searched the net regarding my question.  I continue to play with C off and on over the years, but now I aim to really work my way through the K&R book.
    In the book one sees:
    main()
    hello world stuff stuff
    This used to be no issue at all. It would compile and make a.out with no warning, nor complaint. Now I am working through the K&R book because I want to. I also have the first edition of Mark's Learn C on the Mac, but given how old it, and the second edition is, with regard to Xcode, I'll just stay with the terminal, BBEdit and K&R. By the way, I also saw numerous 1 star ratings on the Learn C book on amazon. Perhaps folks here could tell me how they feel about that book if they have used it and in particular the negative.
    Now I get this warning, and I wonder why main has to have a type; what changed? Some new standard, the mac compiler. Now although I get the warning, default to int and all that, the compiler still creates a.out and it runs fine with the old ./a.out line command.
    kr1.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
    main()
    ^~~~
    1 warning generated.

    Why not just use a properly defined main function? This avoids having the compiler guess about about the intended return type and the number and types of the inputs. The C99 version of Standard C permits the two versions below:
    int main(void) {...}
    int main(int argc, char *arg[]) {...}
    See http://en.wikipedia.org/wiki/Entry_point#C_and_C.2B.2B for more details, and OS specific main variants.
    The last (2:nd) edition of K&R (http://en.wikipedia.org/wiki/The_C_Programming_Language) was released in 1988 preceding standard (ANSI) C and its C89, C95, C99 and C11 variants (http://en.wikipedia.org/wiki/ANSI_C) so K&R is at the very least somewhat out of date with modern C.

  • What is the main functionality of FM 'ISU_S_PAYSCHEME_ROLLIN_ROLLOUT' ?

    Hi Folks,
      I need to use the function module 'ISU_S_PAYSCHEME_ROLLIN_ROLLOUT'. what is the main functionality of this function module? there is no documentation available for this FM.
    Thanks in advance

    Hi,
    Plz follow the link below for answer of your question.
    Link is: Re: how to call standard SAP method in the Custom Program ?
    Thanks & Regards,
    Sarita Singh Rathour

  • CRM FUNCTIONAL interview questions and answers

    Dear Friends,
    Good Afternoon..!!!
    can you tell me the CRM FUNCTIONAL interview questions with answers if avialable.....
    Regards,
    Dhananjaya

    Dear friends,
    Can any body forward me interview questions and answers
    can any body tell me where can i get crm functional video tuter

  • Does ExtendScript have a concept of "main" function/method like when script is included as a library

    I was just wondering whether ExtendScript has any concept like a main method, whereby if the current script is included by another script like a library import it will not execute this main function/method and only execute it when the script is run directly.
    Similar to Java's main() method and Python's __main__.
    If not, is there a workaround to mimic such behavior generically?

    I cannot reproduce this.  Can you create a very simple repro of this issue and post to OneDrive?
    Jeff Sanders (MSFT)
    @jsandersrocks - Windows Store Developer Solutions
    @WSDevSol
    Getting Started With Windows Azure Mobile Services development?
    Click here
    Getting Started With Windows Phone or Store app development?
    Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

  • Travel Management functions interview question and answers

    Hi Guys,
    I wanted to know Travel Management functions interview question and answers , it is most urgent for me.
    Your detail response will be highly appreciated.
    Thanks – Sam.

    Check this site
    http://help.sap.com/saphelp_470/helpdata/en/73/6bf037f1d6b302e10000009b38f889/frameset.htm
    Regards,
    Ruben

  • Oracle Asset (Functional) Practice Questions for Interviews and Exam

    https://www.createspace.com/3495382
    http://www.amazon.com/Functional-Questions-Interviews-Certification-Examination/dp/1456311581/ref=sr_1_4?ie=UTF8&s=books&qid=1289178586&sr=8-4
    List Price: $29.99
    Add to Cart
    Oracle Asset (Functional) Practice Questions for Interviews and Certification Examination (Release 11i and 12)
    Functional Consultant
    Authored by Erp Gold
    This book contains 150 Oracle Asset Practice Questions for functional consultants. Very useful for interviews and certification examinations.
    Publication Date:
    Oct 26 2010
    ISBN/EAN13:
    1456311581 / 9781456311582
    Page Count:
    94
    Binding Type:
    US Trade Paper
    Trim Size:
    6" x 9"
    Language:
    English
    Color:
    Black and White
    Related Categories:
    Computers / General

    Reported as spam!

  • Oracle Asset (Functional) Practice Questions for Interviews and Certificati

    https://www.createspace.com/3495382
    List Price: $29.99
    Add to Cart
    Oracle Asset (Functional) Practice Questions for Interviews and Certification Examination (Release 11i and 12)
    Functional Consultant
    Authored by Erp Gold
    This book contains 150 Oracle Asset Practice Questions for functional consultants. Very useful for interviews and certification examinations.
    Publication Date:
    Oct 26 2010
    ISBN/EAN13:
    1456311581 / 9781456311582
    Page Count:
    94
    Binding Type:
    US Trade Paper
    Trim Size:
    6" x 9"
    Language:
    English
    Color:
    Black and White
    Related Categories:
    Computers / General

    Reported as spam!

  • How to pass argument in main function ?

    How to pass arguments in main function of one class from another class ?
    I don't want to pass argument from command prompt.
    I want to try something like this -
    class Test{
    public static void main(String args[]){
    for(int i=0;i<args.length;i++)
         System.out.println(args);
    class Fun{
    public static void main(String args[]){
         Test t=new Test("Hello","good bye");
    when i run Fun class it gives error.
    Suggest me how can i do that.

    In Fun class main method does not take arguments that is fine. In Test class instead of main method you can have constructor to take input parameters as suggested by BalusC
    However, if you want to make your existing code work, you can call (though not appropriate) main() method of Test class from main() method of Fun class (As main() method is static object is not required to invoke this):
    Test.main(new String[]{"Hello","good bye"});Here is your code:
    class Test{
    public static void main(String args[]){
    for(int i=0;i<args.length;i++)
    System.out.println(args);
    public class Fun{
    public static void main(String args[]){
    //Test t=new Test("Hello","good bye");
    Test.main(new String[]{"Hello","good bye"});
    } Thanks,
    Mrityunjoy                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Help with main function

    Can someone help me out with this? Im trying to create a main function out of this contructor.
    public studentID()
    int size; //hold returned values from box
    int id;
    int age;
    size = Integer.parseInt(JOptionPane.showInputDialog (" Enter a nummber between 1 & 40")); //dialog box
    recCount = 0;
    idArray = new int[size];
    ageArray = new int[size];
    for(int i = 0; i <size; i++) //ask for values (size) times
    id =
    Integer.parseInt(JOptionPane.showInputDialog ("Enter an ID Number"));
    age =
    Integer.parseInt(JOptionPane.showInputDialog ("Enter an age"));
    inputStudent(age, id); //calles method to put values in array
    displayAll();
    selectionSort(idArray, ageArray, size);
    newNumberTable();
    selectionSort(ageArray, idArray, size);
    newAgeTable();
    }

    So what's the problem? Dump it all into the main method.
    Actually I'd advise thinking carefully about whether it should really be in main(). Generally, IMHO, main should only bootstap an app. The real functionality should be in other methods that main() calls.

  • About main function

    When I write the main function,I'm always puzzled by the parameter which is send to the main function.
    For example: main(String args[]) and main(String[] args),What kind of difference is between the "String args[]" and "String[] args"?

    "String args[]" and "String[] args" are exactly equivalent.
    An array of strings is like a bed of roses, or a rosebed: you can take your pick.
    Edited by: pbrockway2 on Apr 2, 2008 6:18 PM
    Ah ha!
    "String args[]" and "String[] args" appears as "String args[]" and "String[] args"

  • Calling main function

    Hello,
    If I want to call the main function of other class, how should I give the parameters of the main function
    public static void main(String args[]) how should I give args[] parameter.
    Thank you

    any use to u?
        public static void invokeMain(String className, String[] args) {
            try {
                Class.forName(className)
                .getMethod("main", new Class[] {String[].class})
                .invoke(null, new Object[] {args});
            } catch (Exception e) {
                InternalError error = new InternalError("Failed to invoke main method");
                error.initCause(e);
                throw error;
        }

  • VS2013 Professional crashing on C++ main function

    Visual Studio loads up just fine, i open the project, create a new source file, and start writing. I write the following with no error:
    #include <iostream>
    using namespace std;
    Then, as i go on to write the main function as follows:
    int main()
    This is when it crashes, it will crash at any point in writing this function. When Windows (7) tells me that it has stopped working, the error information is as follows:
    Problem signature:
    Problem Event Name: CLR20r3
    Problem Signature 01: devenv.exe
    Problem Signature 02: 12.0.31101.0
    Problem Signature 03: 54548724
    Problem Signature 04: Microsoft.VisualStudio.Shell.12.0
    Problem Signature 05: 12.0.31101.0
    Problem Signature 06: 54545916
    Problem Signature 07: a99
    Problem Signature 08: 64
    Problem Signature 09: System.InvalidOperationException
    OS Version: 6.1.7601.2.1.0.768.3
    Locale ID: 2057
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
    Ive tried reinstalling, safemode, administrator, resetting settings, 'repairing', deleting .suo files, nothing works. Same problem.
    I really need this working, but ive run out of ideas. Ive been looking online for a month or so now, and nobody seems to have this problem. It works if I install on a virtual machine, but that is an incredible pain for me.
    If somebody has any idea, it would be a great help. Thanks a lot.

    Hi AlexOgden,
    Thank you for posting in MSDN forum.
    According to your description, as you said that when you write a C++ main function and then the
    VS2013 Professional crashing.
    And you said also that when you write a C++ main function in the VS2013 Professional on a virtual machine, it works fine.
    So to check if this issue is related to the VS IDE environment or your machine environment, I suggest you could try to install a other version of the VS such as the VS2013 Ultimate on your machine and then check if you get the VS crashes issue.
    (1) If you still get the crashes issue in a new VS, I doubt that this issue may be more related to your machine environment, so please try to repair or re-install the OS and then check this issue.
    (2) If you did not get the crashes issue in the new VS, I assume that the issue maybe related to the VS2013 Professional setup file. So I suggest you could try to download this VS2013 Professional set up file from the Microsoft official and then install
    it again check this issue.
    https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx
    Best Regards,
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

Maybe you are looking for