1 Attachment(s)
constructor ABC in class ABC cannot be applied to given types
Task: Create a variable that points to a object of class ABC. And make the object variable (i) =14, and then print it out from main method.
ERROR MESSAGE:
Attachment 4133
Code:
class Prog1{
public static void main(String[]args){
ABC pointer;
pointer=new ABC(14);
System.out.println(pointer.i);
}
}
class ABC {
int i;
}
Re: constructor ABC in class ABC cannot be applied to given types
Basically, that error means that you passed an invalid parameter.
at line 4: pointer = new ABC(14);
That is wrong.
class ABC has no constructor. A constructor is the first method that is called when the a new variable from that class is made.
Everytime you see the parentheses(), know that is a method.
Code:
class ABC{
int i;
public ABC(){
//this is the constructor method.
}
}
But we still aren't done. In order to tell 'new ABC()' to pass the number 14, we have to do this:
Code:
class ABC{
int i;
public ABC(int xyz){ //Now ABC can accept 1 integer value
i = xyz; //This is how we pass that value back to i.
}
Re: constructor ABC in class ABC cannot be applied to given types
My program is fixed, so thank you.
So it's not possible to send a value (14) directly to a objectvariable of class ABC without a constructor or other method?
Re: constructor ABC in class ABC cannot be applied to given types
Quote:
Originally Posted by
AeneasTroy
class ABC has no constructor.
Slightly pedantic point, but it does have one.
A no arguments constructor provided by the compiler.
Re: constructor ABC in class ABC cannot be applied to given types
Quote:
Originally Posted by
AeneasTroy
A constructor is the first method that is called when the a new variable from that class is made.
Also pedantic, but a constructor is not a method.
Not pedantic: correct terminology is important on a technical forum.
db
Re: constructor ABC in class ABC cannot be applied to given types
Quote:
Originally Posted by
DarrylBurke
Also pedantic, but a constructor is not a method.
Not pedantic: correct terminology is important on a technical forum.
db
What is a constructor called?
Re: constructor ABC in class ABC cannot be applied to given types
A constructor is called a constructor. More here.
db