Main class constructor !

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan.J.Thackray

    Main class constructor !

    I am new to Java.

    Since the "main" class has the main() function it it, is this called
    before the constructor, or is a constructor totally irrelevant for a
    main class, since you never "new" it ?
    I am used to C++ whare main exists outside any class.

    I have a test "main" class and it appears the constructor is never
    called ?

    Confused !

    TIA


    Remove the SPAMBLOCK from my email address to reply.
  • Raymond DeCampo

    #2
    Re: Main class constructor !

    Alan.J.Thackray wrote:[color=blue]
    > I am new to Java.
    >
    > Since the "main" class has the main() function it it, is this called
    > before the constructor, or is a constructor totally irrelevant for a
    > main class, since you never "new" it ?
    > I am used to C++ whare main exists outside any class.
    >
    > I have a test "main" class and it appears the constructor is never
    > called ?
    >[/color]

    That is correct. Note that main() is declared static. In this context,
    static means the same as it does in C++; a static method does not need
    to be invoked on an instance of a class.

    Many people who wish to stay in the OO paradigm will write main()
    similar to the following:

    public class MyClass
    {
    // ...other code...

    public static void main(String args[])
    {
    final MyClass myInstance = new MyClass();
    myInstance.go() ;
    }
    }

    HTH,
    Ray

    --
    XML is the programmer's duct tape.

    Comment

    • Hal Rosser

      #3
      Re: Main class constructor !


      "Alan.J.Thackra y" <alan.j.thackra y@SPAMBLOCK.blu eyonder.co.uk> wrote in
      message news:jqrlr0l1uc imh440cgvtkf6aq sbrhqakct@4ax.c om...[color=blue]
      > I am new to Java.
      >
      > Since the "main" class has the main() function it it, is this called
      > before the constructor, or is a constructor totally irrelevant for a
      > main class, since you never "new" it ?
      > I am used to C++ whare main exists outside any class.
      >
      > I have a test "main" class and it appears the constructor is never
      > called ?
      >
      > Confused !
      >
      > TIA[/color]

      Sounds like you actually named your class "main" ????
      You should steer clear of class names like that to avoid confusion.
      Name a class "Bill" or "George" - even "Sue" - use proper case to follow
      convention, and keep away from reserved words.
      --- main is a static method, which means you do not need to create an
      instance of that class to run the method.
      main is *not* a constructor, its a special method.
      HTH



      ---
      Outgoing mail is certified Virus Free.
      Checked by AVG anti-virus system (http://www.grisoft.com).
      Version: 6.0.807 / Virus Database: 549 - Release Date: 12/7/2004


      Comment

      Working...