Results 1 to 5 of 5
  1. #1
    ScotDiddle is offline Member
    Join Date
    Jul 2012
    Location
    Richmond Virginia
    Posts
    2
    Rep Power
    0

    Default public static void main (String [] args) VS. class someName with constructor

    Hello All,

    The only OOP background I have is with PHP V5.3... What's the difference between:

    Java Code:
    public static void main (String [] args) { }
    and

    Java Code:
    public class MyClass {
    
                public MyClass(String string) {
    
                      // TODO add Constructor Code Here
    
                }
    
        }
    Also, why no constructor with
    Java Code:
    void main
    ?

    Thanks, Scot L. Diddle, Richmond VA

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: public static void main (String [] args) VS. class someName with constructor

    One is a static method, so is accessed via the Class, rather than an object of that class.
    The other is a constructor, which is used when a new object is instantiated.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ScotDiddle is offline Member
    Join Date
    Jul 2012
    Location
    Richmond Virginia
    Posts
    2
    Rep Power
    0

    Default Re: public static void main (String [] args) VS. class someName with constructor

    Tolls,

    OK, I understand ( and should have been able to deduce myself, but you know us PHP'ers ).

    So now the question remains, which to use when, and why.

    Thanks in advance.

    Scot L. Diddle, Richmond VA

  4. #4
    gillbates is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: public static void main (String [] args) VS. class someName with constructor

    Hi Scott,

    main is just a method. You have to put it inside of a class to use it. It can be put inside the MyClass class or you can create another class for it.

    A class that contains a static void main method can be executed directly with the java command. Inside the main you can instantiate objects (ObjName nyObj = new ObjName();) in order to execute non-static members or call static members ( methods and instance (think class level) variables initiated with the static keyword).

    Constructors are for classes. Since main is not a class it doesn't get one.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: public static void main (String [] args) VS. class someName with constructor

    Quote Originally Posted by ScotDiddle View Post
    Tolls,

    OK, I understand ( and should have been able to deduce myself, but you know us PHP'ers ).

    So now the question remains, which to use when, and why.

    Thanks in advance.

    Scot L. Diddle, Richmond VA
    You use a constructor to construct objects.
    It allows you to set up an instance of the class so that it is valid for use.

    A static method is one that doesn't need an object to run, or makes no sense to have one. See something like the Math utility class. It's just a load of utility methods for doing maths functions.

    The main() method is special in so much as the signature is what the JVM will look for when trying to run a class, eg on the command line running 'java MyClass' will cause the JVM to look in MyClass for a static main() method. It goes back to C.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. public static void main(String... args)???
    By SnakeDoc in forum New To Java
    Replies: 5
    Last Post: 01-10-2013, 08:30 AM
  2. Q?> about public static void main(String args[])
    By boblingwide in forum New To Java
    Replies: 4
    Last Post: 03-23-2012, 11:17 AM
  3. Replies: 10
    Last Post: 07-10-2011, 08:37 AM
  4. Public static void main (String args[])
    By arefeh in forum New To Java
    Replies: 12
    Last Post: 01-28-2010, 11:58 AM
  5. Replies: 9
    Last Post: 11-22-2008, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •