Results 1 to 8 of 8
- 01-24-2008, 06:18 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 2
- Rep Power
- 0
"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()); } }
Java Code:cannot find symbol constructor Number(int)
Java Code:Number n1 = new Number(15);
- 01-24-2008, 06:28 PM #2
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)
- 01-24-2008, 06:41 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 2
- Rep Power
- 0
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.
- 01-24-2008, 07:33 PM #4
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)
- 01-24-2008, 07:45 PM #5
Member
- Join Date
- Jan 2008
- Posts
- 20
- Rep Power
- 0
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-25-2008 at 12:51 AM. Reason: update
- 01-24-2008, 10:01 PM #6
Member
- Join Date
- Dec 2007
- Posts
- 17
- Rep Power
- 0
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.
- 01-24-2008, 10:37 PM #7
Member
- Join Date
- Jan 2008
- Posts
- 14
- Rep Power
- 0
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
- 01-25-2008, 01:12 AM #8
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
-
Syntax error on token "(", ; expected
By baltimore in forum AWT / SwingReplies: 3Last Post: 10-28-2009, 01:19 AM -
How to solve "No compiler error"?
By iceman in forum New To JavaReplies: 5Last Post: 04-22-2008, 04:37 AM -
Strange error message "Source not found"
By ppayal in forum EclipseReplies: 0Last Post: 11-25-2007, 07:19 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 11:55 PM -
Error: cannot find symbol constructor
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 09:24 PM
Bookmarks