Results 1 to 7 of 7
- 10-05-2012, 12:59 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
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:

Java Code:class Prog1{ public static void main(String[]args){ ABC pointer; pointer=new ABC(14); System.out.println(pointer.i); } } class ABC { int i; }Last edited by Games2Design; 10-05-2012 at 01:01 PM.
- 10-05-2012, 01:14 PM #2
Arma virumque cano
- Join Date
- Oct 2012
- Location
- Indianapolis
- Posts
- 20
- Rep Power
- 0
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.
But we still aren't done. In order to tell 'new ABC()' to pass the number 14, we have to do this:Java Code:class ABC{ int i; public ABC(){ //this is the constructor method. } }
Java 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. }
- 10-05-2012, 01:37 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
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?Last edited by Games2Design; 10-05-2012 at 01:45 PM.
- 10-05-2012, 01:58 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 10-05-2012, 04:39 PM #5
- 10-05-2012, 10:47 PM #6
Arma virumque cano
- Join Date
- Oct 2012
- Location
- Indianapolis
- Posts
- 20
- Rep Power
- 0
- 10-06-2012, 02:21 AM #7
Re: constructor ABC in class ABC cannot be applied to given types
A constructor is called a constructor. More here.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Error: Cannot be applied to given types. How do I solve this?
By BenH in forum New To JavaReplies: 11Last Post: 02-01-2012, 04:27 AM -
error: Constructor 'X' in Class 'X' cannot be applied to given types
By FiasseKrystella in forum New To JavaReplies: 3Last Post: 10-01-2011, 09:29 PM -
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks