Results 1 to 5 of 5
- 07-03-2014, 05:50 PM #1
Member
- Join Date
- Jul 2014
- Posts
- 2
- Rep Power
- 0
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 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); } }
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.
- 07-03-2014, 05:53 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 07-03-2014, 06:07 PM #3
Re: How to call a constructor in main
Crossposted: 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!
- 07-03-2014, 06:51 PM #4
Member
- Join Date
- Jul 2014
- Posts
- 2
- Rep Power
- 0
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.
- 07-03-2014, 06:58 PM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Similar Threads
-
call default constructor inside of parameterized constructor after conditional check
By Googol in forum New To JavaReplies: 5Last Post: 08-11-2012, 10:50 AM -
How to call a master constructor from a minor constructor?
By b.m in forum New To JavaReplies: 5Last Post: 12-14-2011, 02:47 PM -
Why do I need to call my constructor to get this to work?
By fresh83 in forum New To JavaReplies: 7Last Post: 10-28-2011, 08:26 AM -
Unable to call classe constructor from main
By serdimay in forum New To JavaReplies: 13Last Post: 08-30-2011, 11:38 AM -
Constructor call not calling
By Singing Boyo in forum New To JavaReplies: 5Last Post: 06-09-2009, 02:06 AM
Bookmarks