Results 1 to 7 of 7
  1. #1
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default 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:
    constructor ABC in class ABC cannot be applied to given types-error.png


    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 02:01 PM.

  2. #2
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default 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.


    Java 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:

    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.
    
    }

  3. #3
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default 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 02:45 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: constructor ABC in class ABC cannot be applied to given types

    Quote Originally Posted by AeneasTroy View Post
    class ABC has no constructor.
    Slightly pedantic point, but it does have one.
    A no arguments constructor provided by the compiler.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: constructor ABC in class ABC cannot be applied to given types

    Quote Originally Posted by AeneasTroy View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: constructor ABC in class ABC cannot be applied to given types

    Quote Originally Posted by DarrylBurke View Post
    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?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: constructor ABC in class ABC cannot be applied to given types

    A constructor is called a constructor. More here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 11
    Last Post: 02-01-2012, 05:27 AM
  2. Replies: 3
    Last Post: 10-01-2011, 10:29 PM
  3. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  4. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •