Results 1 to 5 of 5
  1. #1
    ymbulk is offline Member
    Join Date
    Jul 2014
    Posts
    2
    Rep Power
    0

    Default How to call a constructor in main

    Hey guys, I am trying to call a constructor from PrepaidCard class in my main method, but I am not sure how to proceed.

    As seen below, both the PrepaidCard constructor and the setCardID method have the ability to set the card ID.
    Java Code:
    public class PrepaidCard
    {
            public PrepaidCard(String id, int token)
    	{
    		cardID = id;
    		tokenBalance = token;
    	}
            public void setCardID(String id, int token)
    	{
    		cardID = id;
    		tokenBalance = token;
    	}
    }
    Now in this block of code, I can successfully pass the id and token value by calling the setCardID method from the PrepaidCard class.
    Now I would like to call the PrepaidCard constructor from the PrepaidCard class to pass the id and token value, instead of using the setCardID method.
    Java Code:
    public class PrepaidCardTest
    {
    	public static void main(String[] args)
    	{
    		PrepaidCard card2 = new PrepaidCard(id, token);
                    
                    System.out.print("Enter Card2 cardID: ");
    		id = input.nextLine();
    		card2.setCardID(id, token);
            }
    }
    Does anyone know how to call the PrepaidCard constructor from the PrepaidCard class, to successfully pass the id and token value, in my main method?
    Specifically how to modify or replace this line of code so that it can correctly call the PrepaidCard constructor?
    Java Code:
    card2.setCardID(id, token);
    Last edited by ymbulk; 07-03-2014 at 06:02 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: How to call a constructor in main

    You don't call the constructor, the JVM does that for you when you use the new keyword to create the object.

    So get the ID and token first, then construct the PrepaidCard object in the way you're already doing now.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    16

    Default Re: How to call a constructor in main

    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  4. #4
    ymbulk is offline Member
    Join Date
    Jul 2014
    Posts
    2
    Rep Power
    0

    Default Re: How to call a constructor in main

    Thanks for your reply and reminder of cross posting.
    I have omitted the declaration of the variables, but if you want, I can update the code to make it compile.
    Do you know how to call the PrepaidCard constructor from the PrepaidCard class? So that I won't have to call the setCardID method, or is it not possible to call a constructor like that?
    I would like to replace the setCardID method with the PrepaidClass constructor to pass the id and token, or is it not possible to do that and I have to use the setCardID method? Because in my assignment, it only supplies the PrepaidCard constructor and not the setCardID method.
    Both of these (PrepaidCard constructor and setCardID method) are in the PrepaidCard class. If I can successfully pass the id and token to the PrepaidCard constructor, then I can delete the setCardID method as it is of no use.
    Quote Originally Posted by KevinWorkman View Post

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: How to call a constructor in main

    You just copy pasted what you replied in your cross-post! That's just sad.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 10:50 AM
  2. Replies: 5
    Last Post: 12-14-2011, 02:47 PM
  3. Replies: 7
    Last Post: 10-28-2011, 08:26 AM
  4. Unable to call classe constructor from main
    By serdimay in forum New To Java
    Replies: 13
    Last Post: 08-30-2011, 11:38 AM
  5. Constructor call not calling
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-09-2009, 02:06 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
  •