Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to call a constructor in main

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.
    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.
    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?
    card2.setCardID(id, token);


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: How to call a constructor in main

    I'm not entirely sure what you're asking, but the code you posted won't compile.

    You're using variables named id and token, but you haven't initialized them to anything.

    If I were you, I would start smaller: Can you write a program that asks the user for an id and token, saves them to variables, and then simply prints those values back out to the console?

    When you have that working, then you can expand it to call the constructor with those variables instead of printing those variables out.

    --- Update ---

    Nevermind.

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/90395-how-call-constructor-main.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,775
    My Mood
    Mellow
    Thanks
    235
    Thanked 715 Times in 701 Posts

    Default Re: How to call a constructor in main

    Post the PrepaidCard class.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to call a constructor in main

    This is the PrepaidCard class.
    public class PrepaidCard
    {
            private String cardID;
    	private int tokenBalance;
     
            public PrepaidCard(String id, int token)
    	{
    		cardID = id;
    		tokenBalance = token;
    	}
            public void setCardID(String id, int token)
    	{
    		cardID = id;
    		tokenBalance = token;
    	}
    }
    Quote Originally Posted by GregBrannon View Post
    Post the PrepaidCard class.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,775
    My Mood
    Mellow
    Thanks
    235
    Thanked 715 Times in 701 Posts

    Default Re: How to call a constructor in main

    You're getting compiler errors, right? What are they and what have you tried to do to correct them?

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to call a constructor in main

    I compile the program fine, just that I would like to call the PrepaidCard constructor instead of calling the setCardID method.
    Do you know if it is possible to do that?
    Quote Originally Posted by GregBrannon View Post
    You're getting compiler errors, right? What are they and what have you tried to do to correct them?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,775
    My Mood
    Mellow
    Thanks
    235
    Thanked 715 Times in 701 Posts

    Default Re: How to call a constructor in main

    Like this:
    public static void main(String[] args)
    {
        // initialize id
     
     
        // initialize token
     
     
        PrepaidCard card2 = new PrepaidCard(id, token);
     
    } // end method main()

Similar Threads

  1. how to open one Jframe from main method call
    By ankushpruthi in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 1st, 2013, 05:23 PM
  2. Replies: 0
    Last Post: December 16th, 2012, 06:49 AM
  3. Why cant I call on the array I returned in the main method?
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 03:08 PM
  4. How to call a value from a different method to main
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 22nd, 2011, 06:29 PM
  5. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM