#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Constructor help


    Hi guys,

    I can't get this constructor to compile. It keeps telling me either a I need a return type. I thought constructors didn't need them? Or ')' expected.

    public numbers(int[] array = {int firstNumber, int secondNumber, int thirdNumber}; anArray)
    {
    this.NumberList = numbers;
    }

    Any advice? Cheers
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    I have no idea what you're trying to pass as a parameter - that isn't close to valid syntax. Can you tell us what you're trying to do?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by stdunbar
    I have no idea what you're trying to pass as a parameter - that isn't close to valid syntax. Can you tell us what you're trying to do?
    I'm trying to initialize an array of numbers to be stored in a local variable using that constructor.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Originally Posted by Twitcher
    I'm trying to initialize an array of numbers to be stored in a local variable using that constructor.
    The syntax is incorrect.

    The parameter list is a sequence of variable declarations.

    You don't put code in the parameter list.
  8. #5
  9. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    Are you sure you're programming in Java? You'd want something more like:

    java Code:
    public numbers( int[] anArray)


    as the constructor definition. You'll have to place the values into the array when you call it:

    java Code:
    int[] theArray = { 10, 20, 30 };
    numbers theNumbers = new numbers( theArray );


    Lastly, it is traditional and considered good style for your class names to begin with upper case and your member variables to begin with lower case. It looks like you've got that backwards. Note that that is not required by the Java language though.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    Thanks stdunbar! That looks like it will work.

    I'm still getting told by the IDE I need to put a return type in the method header. I thought constructors didn't need return types?
  12. #7
  13. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    Originally Posted by Twitcher
    I'm still getting told by the IDE I need to put a return type in the method header. I thought constructors didn't need return types?
    Then you're not creating a constructor. In Java a constuctor must take the same name as the class, including case. Is your class named numbers?

    The code below compiles fine:

    java Code:
    public class numbers {
        public static void main( String[] argv ) {
            int[] theArray = { 10, 20, 30 };
     
            numbers theNumbers = new numbers( theArray );
        }
     
        public numbers( int[] anArray ) {
        }
    }
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    I think that has fixed it for me!!!! Thanks!

    I still have syntax errors in the code but it's moved on from that now.

    The book I'm learning from has never mentioned the constructor must take the same name as the class. Sounds like a bit of an oversight that doesn't it!

    Can you recommend a decent comprehensive text book I can learn from?
  16. #9
  17. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    I usually direct people to the source - The Java Tutorials. If you look about half way down on that page you'll see a section labeled "Trails Covering the Basics". It sounds like you're past the "Getting Started" section as you have things installed. Under "Learning the Java Language" there is a section for "Classes and Objects" that includes a section on constructors.

    In general these tutorials will teach you everything but they can be a bit hard to read at times as they are either too deep or too shallow. My advice would be to read some stuff, and try it out in your IDE if it doesn't make sense. Learn, experiment, repeat.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    Thanks, that looks like a handy resource. I wish I'd known about it a few months ago.

    Do you have any idea why the IDE is telling me I need a semicolon after l in this piece of code please?

    @Override
    public String toString()
    {
    int len = numbers.length;
    for (int l = 0; l <= len; l++)
    {
    Return l(it is asking for a semi colon here) + "(" + len + ")" + " ";
    }
    }
  20. #11
  21. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    For one thing, Java is case-sensitive so you would use return.

    Also I don't think you intended to put the return statement where is it.

    A return statement means the method will exit. But the compiler is going to complain about a missing return statement because it is possible that the condition on the for loop won't be true so its possible the return won't execute.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    Cheers bullet!

    That's seems like some good advice I can work off.

    Again, the source I'm learning java from hasn't told me a return statement will mean the method is exited. That seems like a pretty simple thing that should of been mentioned.

IMN logo majestic logo threadwatch logo seochat tools logo