This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Cannot find constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot find constructor" Watch "Cannot find constructor" New topic
Author

Cannot find constructor

Ben Calvin
Greenhorn

Joined: Feb 07, 2013
Posts: 12
Class



Demo


Error of cannot find symbol
Symbol: contstructor Teamleader()
Location: class TeamLeader leader1 = -->n <-- ew TeamLeader();

I thought I was doing everything right for the first time, my TeamLeader class even compiles, but when i try to draw from it, no bueno. Please help.
Ben Calvin
Greenhorn

Joined: Feb 07, 2013
Posts: 12
Why am I unable to edit posted thread, the file is missing two } at the end..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Shall I add the }}? You ought to be indenting your code; that is the point of code tags, to maintain indentation. I presume you have found the preview button, so you can see what the post looks like before committing it.

The reason it won’t compile is that you need to pass arguments to the constructor. Your class requires 5 arguments, so you must pass 5 arguments.
By the way, those arguments are confusing. 5 counts as a lot, and the names you have given (ep) are not at all informative. There is a risk of getting the arguments out of order, particularly with so many of them.
Why is training required an int? There is some dubious logic in using a number for that.
Antriksh jain
Greenhorn

Joined: Feb 22, 2013
Posts: 2
Hello ben.

The problem with your code is you have use parameterized construtor.
public TeamLeader(int ep, int pn, double b, int tr, int ta)
{
prodNum = pn;
empProd = ep;
bonus = b;
trainingRequired = tr;
trainingAttended = ta
}

but your defalult contructor is missing. Add code before parameterized contrutor as:
public TeamLeader()
{
}
By default java provide a default contrutor.But if are defining contrutor from your own it will not provide any default contrutor.

let me know it is not working

Thanks.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Antriksh jain wrote: . . .. Add code before parameterized contrutor as:
public TeamLeader()
{
}
. . .
No, don’t do that at all.
Unless having 0 for all those values is a valid state for the object to be in, do not provide a no‑args constructor alongside your several‑args constructor.
You will have been told that constructors are there to allow creation of an object. They are also there to restrict object creation: in this case it only works if you supply the 5 parameters.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Welcome to the Ranch Antriksh jain.
I would have added code tags to your post, which you should always use, but you hadn’t indented the code, so it wouldn’t have improved it much.
Ben Calvin
Greenhorn

Joined: Feb 07, 2013
Posts: 12
Campbell Ritchie wrote:
Antriksh jain wrote: . . .. Add code before parameterized contrutor as:
public TeamLeader()
{
}
. . .
No, don’t do that at all.
Unless having 0 for all those values is a valid state for the object to be in, do not provide a no‑args constructor alongside your several‑args constructor.
You will have been told that constructors are there to allow creation of an object. They are also there to restrict object creation: in this case it only works if you supply the 5 parameters.


gimme a second i'll try it out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
TeamLeader tl = new TeamLeader(123, 234, 345.67, 678, 789);
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
You will have problems because you have not overridden the toString() method. It will compile and run and give you a disappointing output.
Ben Calvin
Greenhorn

Joined: Feb 07, 2013
Posts: 12
so.... after looking at what you guys spoke of something clicked in my head. I was making something so simple way too complicated.

I went from the code you see above to....



The demo now works ;)
Ben Calvin
Greenhorn

Joined: Feb 07, 2013
Posts: 12
thank you everyone.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
You’re welcome
If I were marking it, I would take lots of marks off for the poor field names. Also for inappropriate use of Strings for the bonus.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
The demo now works ;)

Not as posted I'm afraid, unless you have a written your own class called 'string'

Your TeamLeader class extends PorductionWorker. I think this should be ProductionWorker.
Ben Calvin
Greenhorn

Joined: Feb 07, 2013
Posts: 12
I am more then open to improvement. I don't understand the string comment
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

When you check over your code, you should check for capitalization.

What Tony was referring to is that String should be capitalized.

Also in your first post where it says it can't find "Teamleader()", the "L" should have been capitalized and be passed the appropriate arguments.


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Ben Calvin
Greenhorn

Joined: Feb 07, 2013
Posts: 12
Thank you for the clarity. The mistakes pointed out were, besides the horrendous naming convention, due to me retyping the entire files. Everything works as planned. I will look more into constructors further besides the adding the statements in the class. I think I will review the early chapters on naming convention as well. This place is a really good resource.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
It's very important when posting code to a forum that you do it via copy and paste rather than typing it in. If you type it in and make a mistake (which is easy to do as you have discovered) then you are wasting our time getting us to help you solve a problem that isn't even in the code you are running.
 
 
subject: Cannot find constructor
 
Similar Threads
Compiling Errors - Help please
Cannot instantiate the type?
Setters
Derived Class & Method Calling
Casting problem