Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-24-2008, 06:18 PM
Member
 
Join Date: Jan 2008
Posts: 2
Welsh is on a distinguished road
"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:
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:
Code:
cannot find symbol constructor Number(int)
on line 15, which is
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-24-2008, 06:28 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-24-2008, 06:41 PM
Member
 
Join Date: Jan 2008
Posts: 2
Welsh is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-24-2008, 07:33 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-24-2008, 07:45 PM
Member
 
Join Date: Jan 2008
Posts: 20
JAdmin is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-24-2008, 10:01 PM
Member
 
Join Date: Dec 2007
Posts: 17
Jman is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-24-2008, 10:37 PM
Member
 
Join Date: Jan 2008
Posts: 7
frejon26 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-25-2008, 01:12 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
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.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to solve "No compiler error"? iceman New To Java 5 04-22-2008 04:37 AM
Strange error message "Source not found" ppayal Eclipse 0 11-25-2007 07:19 PM
Syntax error on token "(", ; expected baltimore AWT / Swing 1 08-01-2007 12:34 AM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-25-2007 11:55 PM
Error: cannot find symbol constructor zoe New To Java 1 07-24-2007 09:24 PM


All times are GMT +3. The time now is 04:53 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org