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.

class Student {
	
	private String name, add,dit;

	public String getname() 
	{
		return name; 
	}
	
	public String getadd() 
	{
		return add; 
	}

	public String getdit() 
	{
		return dit; 
	}

	public void setname(String n) 
	{
		this.name=n; 
	}
	
	public void setadd(String a) 
	{
		this.add=a; 
	}

	public void setdit(String d) 
	{
		this.dit=d; 
	}


	public String getdetails()
	{
		return "i am a student "+"my name is "+this.name +"i am from "+this.add+"my dit no is "+this.dit;
  	}
	
}


class ex4{

	public static void main(String[] args)
	{

	Student s1 = new Student("kamal", "kandy", "5678");

	System.out.println(s1.getdetails());
	
	}
}

ERROR

ex4.java:49: cannot find symbol
symbol  : constructor Student(java.lang.String,java.lang.String,java.lang.String
)
location: class Student
        Student s1 = new Student("kamal", "kandy", "5678");
                     ^
1 error

thanks in advance...

You need to declare the parameterized constructor.

You need to declare the parameterized constructor.

im sorry...can u explain this with more details....
thanks...

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:

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:

Student s1 = new Student("kamal", "kandy", "5678");
System.out.println(s1);

Try printing this:
System.out.println(s1) without defining it and then create that method (toString) and try again.

commented: thanks... +0

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:

class DaniwebMember{
private int reputation;
private int posts;
private string username;
// other stuff 

//parameterized constructor 
public DaniwebMember(){
    reputation = 10;
    posts = 0; // need not do this, since the default is 0 anyway.
    // other init stuff
}

// other methods
}

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

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.

commented: thanks... =) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.