function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Navneeth RajNavneeth Raj 

What's the error Here? Constructor not defined: [ConsWithPara].<Constructor>()

User-added image
When Execute is clicked error comes as below

Line: 1, Column: 17
Constructor not defined: [ConsWithPara].<Constructor>()
Amit Chaudhary 8Amit Chaudhary 8
Hi Raj,

You are using parameterize constructor in your main class. And in developer console you are creating object like below
ConsWithPare obj = new ConsWithPare ();

Please try below code
 
ConsWithPare obj = new ConsWithPare (10);
obj.calc(200);
obj.display()


Please let us know if this will help you.

Thanks
Amit Chaudhary
Prashant WayalPrashant Wayal
Hi Navneeth,

The error indicate you have not defined a default constructor in your class.

Here, in apex code where you are creating object for this class (line 1), you are using default constructor which is not defined in class. Only one parameterized constructor is defined. just add below line in your class to solve this problem:
 
public ConsWithPara(){}


Hope this helps you,

Regards,
Prashant
KaranrajKaranraj
Constructor has to called while initating your class, you can't call like an normal method. Try the below code
consWithPara cp=new consWithPara(25);
cp.calc(9000);
cp.display();
Only static method can be called with ClassName.StaticMethodName(); to call normal methods you have to initate the class and call the the class method with the instance name.
 
Agustina GarciaAgustina Garcia
Hi,

If you don't define any constructor in your class, then there is a default one with no arguments. However you have already defined one with an argument:
 
public ConsWithPara(integer age)
 {
        this.age=age;
 }

So, or you create another one without arguments, or you would need to use this one.

Your second line, would also fail, because you are trying to call the constructor after instansciate it. And the same with the 3rd, you are trying to call cal method with the name of the class. If the method where static, you would execute the 3rd like like you try but as they are not static, it should be with cp variable. In addition, your cal method is void, so you cannot assign it to a variable. Finally display call is the same, it shoudl be like this your call:
 
ConsWithPara cp = new ConsWithPara(5);
//Integer a= cp.ConsWithPara(25);
cp.cal(900);
cp.display();

Hope this helps