Results 1 to 8 of 8
  1. #1
    Welsh is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default "Cannont find symbol Constructor" error

    Hey, i have a much larger program however it was giving me this 1 error, i tried to just get that 1 part working however i keep failing. I know im probally missing something very simple. My code is:
    Java Code:
    class Number {
      private int number = 0;
      
      public void Pie(int numIn){
      	number = numIn;
      }
    
      public int getNum(){
      	return number;
      }
    }
    
    public class Test {
      public static void main(String[] arg) {
      	Number n1 = new Number(15);
    	System.out.println(n1.getNum());
      }
    }
    Whenever i try to compile i get this error:
    Java Code:
    cannot find symbol constructor Number(int)
    on line 15, which is
    Java Code:
    Number n1 = new Number(15);
    so for some reason my code doesnt like me creating a new class. Any suggestions on how i can fix this, probally something very simple im just forgetting to do.

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Welcome to the Java Forums Welsh.

    Your error message is pretty clear on what's wrong... simply put, you don't have the one-argument constructor implemented. :) The error message even tells you the partial signature of the constructor.

    See you around!
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    Welsh is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default

    and how do i implement the one-argument constructor...im still learning java and any help would be great and thank you for the welcome Captain.

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    You didn't think I'd actually provide the flat-out answer... did you? That's just not my MO :-D Seriously, do you own a Java book? Either way, I always recommend Head First Java, for starters - but there are many other choices. If you'd rather not read a book, I've taken the liberty of linking to the specific topic in the Sun Java Tutorials, your answer is there. You must read and research this topic as its so fundamental and critical in Java. Let us know what you come up with. :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  5. #5
    JAdmin is offline Member
    Join Date
    Jan 2008
    Posts
    20
    Rep Power
    0

    Default

    As explained by the previous users ,you need to have a constructor that taked an argument to make call like new Number(15).

    Now, if you want to still use the same program, change the call like
    Number n1 = new Number();
    ni.Pie(15);
    System.out.println(n1.getNum());

    Note, that we have used new Number(). Every class has a default constructor with no argument and it gets called, when we create object as above.

    Hope this helps!
    Last edited by JAdmin; 01-24-2008 at 11:51 PM. Reason: update

  6. #6
    Jman is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Hi Welsh

    I had a look at your code and it will work if you change one small thing. have a read about Constructors. You could make a public Number() constructor.

    Jman.

  7. #7
    frejon26 is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default

    as everyone else said you can use Java's default constructor.
    Number n1 = new Number();
    or alter the "Pie" method which looks as if it is practically a constructor anyway just the wrong syntax. remove void and replace "Pie" with the name of your class. As in:

    public Number(int numIn){
    number = numIn;
    }
    I think conventionally only Constructor methods should be uppercase.

    -Jon

  8. #8
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Yeah Welsh you must understand that constructors have the same name as the class. Say the class is Number, the constructor is: public Number(int num)
    You have not defined this constructor, so in your main class, you are passing an int to a constructor whose parameter type is void (eh, i'm not sure if you can say that in java, but that's what it is in C++ anyway haha). What i'm saying is. See how everyone said that the default constructor is public Number(), if you want to make it so that you can construct a new object of the Number type whose argument calls for an int value, you must make this constructor. Say for example, we made a constructor in this class: public Number(String value)... if we tried to pass a char into this it wouldn't work, because there is no constructor in the Number class that calls for a char.

Similar Threads

  1. Syntax error on token "(", ; expected
    By baltimore in forum AWT / Swing
    Replies: 3
    Last Post: 10-28-2009, 12:19 AM
  2. How to solve "No compiler error"?
    By iceman in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 03:37 AM
  3. Strange error message "Source not found"
    By ppayal in forum Eclipse
    Replies: 0
    Last Post: 11-25-2007, 06:19 PM
  4. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  5. Error: cannot find symbol constructor
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:24 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
  •