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 9 of 9

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,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 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,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 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,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 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,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 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()

  9. #9
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: How to call a constructor in main

    To call the constructor of the `PrepaidCard` class in your `main` method, you need to use the `new` keyword followed by the class name and parameters required by the constructor. In this case, the constructor of `PrepaidCard` takes two parameters: `id` and `token`. Here's how you can modify the line of code to call the constructor instead of the `setCardID` method:

    ```java
    PrepaidCard card2 = new PrepaidCard(id, token);
    ```

    This line of code creates a new instance of the `PrepaidCard` class, invoking its constructor with the provided `id` and `token` values. After this line, you don't need to call the `setCardID` method because the constructor has already initialized the `cardID` and `tokenBalance` fields with the specified values. So, you can remove the following line:

    ```java
    card2.setCardID(id, token);
    ```

    With these changes, your main method should correctly initialize a new PrepaidCard object. If you need further assistance with Java assignments, there are many resources available online, such as programminghomeworkhelp.com, where you can find help with Java assignment.

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, 07: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, 04: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