942,956 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1244
  • Java RSS
Feb 9th, 2010
0

Problem with constructors - Error "Cannot find symbol constructor" NEW TO JAVA

Expand Post »
Hey all..am quite new to Java and I have encountered a problem with my program. I am getting the error message "cannot find symbol constructor". I have tried to figure it out but I have failed. I have read around that adding an empty constructor would help but this is not working either so I am just confused. any help is needed.thanks. Below is the entire code.

Java Syntax (Toggle Plain Text)
  1. class Student {
  2.  
  3. private String name, add,dit;
  4.  
  5. public String getname()
  6. {
  7. return name;
  8. }
  9.  
  10. public String getadd()
  11. {
  12. return add;
  13. }
  14.  
  15. public String getdit()
  16. {
  17. return dit;
  18. }
  19.  
  20. public void setname(String n)
  21. {
  22. this.name=n;
  23. }
  24.  
  25. public void setadd(String a)
  26. {
  27. this.add=a;
  28. }
  29.  
  30. public void setdit(String d)
  31. {
  32. this.dit=d;
  33. }
  34.  
  35.  
  36. public String getdetails()
  37. {
  38. return "i am a student "+"my name is "+this.name +"i am from "+this.add+"my dit no is "+this.dit;
  39. }
  40.  
  41. }
  42.  
  43.  
  44. class ex4{
  45.  
  46. public static void main(String[] args)
  47. {
  48.  
  49. Student s1 = new Student("kamal", "kandy", "5678");
  50.  
  51. System.out.println(s1.getdetails());
  52.  
  53. }
  54. }

ERROR
Java Syntax (Toggle Plain Text)
  1. ex4.java:49: cannot find symbol
  2. symbol : constructor Student(java.lang.String,java.lang.String,java.lang.String
  3. )
  4. location: class Student
  5. Student s1 = new Student("kamal", "kandy", "5678");
  6. ^
  7. 1 error


thanks in advance...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scratchwiz is offline Offline
6 posts
since Feb 2010
Feb 9th, 2010
0
Re: Problem with constructors - Error "Cannot find symbol constructor" NEW TO JAVA
You need to declare the parameterized constructor.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Feb 9th, 2010
0
Re: Problem with constructors - Error "Cannot find symbol constructor" NEW TO JAVA
Click to Expand / Collapse  Quote originally posted by verruckt24 ...
You need to declare the parameterized constructor.
im sorry...can u explain this with more details....
thanks...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scratchwiz is offline Offline
6 posts
since Feb 2010
Feb 9th, 2010
1
Re: Problem with constructors - Error "Cannot find symbol constructor" NEW TO JAVA
You are calling a constructor that does not exist. It is like calling a method with the wrong number or arguments. You haven't declared that kind of constructor:
Java Syntax (Toggle Plain Text)
  1. Student s1 = new Student("kamal", "kandy", "5678")

Also if you paid attention to the errors that you get, you should be able to figure that on your own.

Also it would be better to rename the method getdetails to toString.

It is inherited from the Object class and is called automatically when you do this:
Java Syntax (Toggle Plain Text)
  1. Student s1 = new Student("kamal", "kandy", "5678");
  2. System.out.println(s1);

Try printing this:
System.out.println(s1) without defining it and then create that method (toString) and try again.
Last edited by javaAddict; Feb 9th, 2010 at 11:12 am.
Sponsor
Featured Poster
Reputation Points: 1005
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,256 posts
since Dec 2007
Feb 9th, 2010
1
Re: Problem with constructors - Error "Cannot find symbol constructor" NEW TO JAVA
A constructor is a method that is used to "construct" a new instance of an object. You put various initialization stuff in there such as values that some of your variables should be initialized with. When you are constructing an object you sometimes require that the object shouldn't be constructed with the default values for the instance variables but something other that, something of your choice. To give you an example assume the construction of a Daniweb member, when the member object is constructed it is already alloted with 10 reputation points and not the default 0. So you would implement this as:
Java Syntax (Toggle Plain Text)
  1. class DaniwebMember{
  2. private int reputation;
  3. private int posts;
  4. private string username;
  5. // other stuff
  6.  
  7. //parameterized constructor
  8. public DaniwebMember(){
  9. reputation = 10;
  10. posts = 0; // need not do this, since the default is 0 anyway.
  11. // other init stuff
  12. }
  13.  
  14. // other methods
  15. }

Constructors can be parametrized or without any parameters. Those that accept parameters are known as parameterized constructors. There could also be multiple constructors for a single class indicating that the object could be initialized in multiple ways. This you can find out if you surf the Java APIs for some classes. If you do not provide a constructor for your class, Java will provide one for you which is known as the default constructor which will obviously not accept any parameters. But if you do provide a parameterized one it won't provide a default non-parameterized one for you. This is because it assumes the developer knows what he is doing and that may be the only way he wants his objects created.

Well thats about some concepts, coming back to your problem, you haven't defined the parameterized constructor that you are implicitly placing a call to. (through the use of the new keyword) on this line
Java Syntax (Toggle Plain Text)
  1. Student s1 = new Student("kamal", "kandy", "5678");
on seeing this the compiler expects a constructor with three string parameters to be defined which it doesn't find and hence the error.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help if You Can !
Next Thread in Java Forum Timeline: Client Server Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC