Results 1 to 6 of 6
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Question on constructors

    Hey guys, I had a question on constructors. Say you set up a class, "Apples". You make a constructor with
    Java Code:
    String s
    as a parameter. Now, you need to run a method from the main method. I know that you make an object within the main method, but what would you fill in for the constructor? would you say
    Java Code:
    Apples red = new Apples(void);
    or
    Apples red = new Apples();
    , and just leave the constructor blank? Very confused.

    Thanks for all answers guys!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,654
    Rep Power
    5

    Default Re: Question on constructors

    Your question is a bit confusing. It sounds at first like you want to invoke a constructor with a String as an argument (perhaps you meant method). Then you ask about invoking an empty constructor. In any event, the proper default constructor syntax (no arguments) is your second example.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question on constructors

    Quote Originally Posted by jim829 View Post
    Your question is a bit confusing. It sounds at first like you want to invoke a constructor with a String as an argument (perhaps you meant method). Then you ask about invoking an empty constructor. In any event, the proper default constructor syntax (no arguments) is your second example.

    Regards,
    Jim
    No I am saying that I made a constructor, and now when I need to create an object to call the method that I want, which is non static, I don't know whether to assign values to the object, or put void in the parentheses. Thanks

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,654
    Rep Power
    5

    Default Re: Question on constructors

    Like this:

    Java Code:
    Apples red = new Apples();
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Question on constructors

    And you don't instantiate an aobject just to call a method on it.
    You instantiate it because you need the object for something.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Question on constructors

    Quote Originally Posted by MW130 View Post
    Java Code:
    Apples red = new Apples(void);
    You cannot pass void but you can pass null. However if you need to construct an object that does not take an initial value then the common practice is to have 2 (or more) constructors.
    Java Code:
    class Foo {
        private String s;
    
        Foo() {
            s = "Default";
        }
    
        Foo(String param) {
            s = param;
        }
    
        public static void main(String[] arg) {
            Foo one = new Foo();
            Foo two = new Foo("special");
        }
    }

Similar Threads

  1. Help with constructors
    By monopolyman900 in forum New To Java
    Replies: 1
    Last Post: 03-04-2013, 12:21 AM
  2. Help with constructors
    By philip1597 in forum New To Java
    Replies: 7
    Last Post: 08-31-2012, 01:56 PM
  3. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  4. Constructors
    By suresh.sa in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 12:10 AM
  5. Class and constructors question.
    By Meta in forum New To Java
    Replies: 19
    Last Post: 04-29-2010, 02:58 AM

Tags for this Thread

Posting Permissions

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