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

Thread: problem with instantiating object

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default problem with instantiating object

    Hi, I'm getting an error that an object cannot be instantiated in the following code
    public class Prog2 {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String [] args){
            Card card = new Card(Rank.QUEEN, Suit.SPADES);
        //System.out.println(card); 
        //System.out.println(card.toStringShort()); 
        System.out.println("_____________________________"); 
       /* Deck deck = new Deck(); 
        deck.shuffle(); 
        for (int i = 0; i < 50; ++i) {
            System.out.println(deck.dealCard());
    }
        System.out.println(deck); //prints the remaining cards
        System.out.println("_____________________________");
        Shoe shoe = new Shoe(4);
        shoe.shuffle();  
        for (int i = 0; i < 50; ++i) {
            System.out.println(shoe.dealCard());
        } 
     
    System.out.println("_____________________________"); */
    }
    }
        class Card{ //outputs info for a single instance of a card
           public int SingleCard;
           public int SingleSuit;
            public Card(int x, int y) {
        SingleCard = x;
        SingleSuit = y;
        }
        class Deck{
     
        }
        class Shoe{
     
        }
        class Rank{
            public static final int ACE=0;
            public static final int TWO=1;
            public static final int THREE=2;
            public static final int FOUR=3;
            public static final int FIVE=4;
            public static final int SIX=5;
            public static final int SEVEN=6;
            public static final int EIGHT=7;
            public static final int NINE=8;
            public static final int TEN=9;
            public static final int JACK=10;
            public static final int QUEEN=11;
            public static final int KING=12;
     
        }
        class Suit{
            public static final int SPADES = 1;
            public static final int HEARTS = 0;
            public static final int CLUBS = 3;
            public static final int DIAMONDS = 2;
        }}

    I keep getting this error:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - internal error; cannot instantiate Card(int,int) at prog2.Card to ()
    at prog2.Prog2.main(Prog2.java:7)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)

    ---
    When I tried replacing Rank.QUEEN and Suit.SPADES with integer values instead the program had no problem.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: problem with instantiating object

    Do you want Rank and Suit to be inner classes of Card? If so (as is currently written), you need to specify this when referring to them (eg Card.Rank.QUEEN)

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: problem with instantiating object

    I'm trying to avoid that, the idea is to pass values from QUEEN in the rank class and SPADES in the Suit class into the the new Card(int, int) statement instead of something like new Card(3,4)

    If I wrote something in the code that means that, then maybe that's my error.

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: problem with instantiating object

    Quote Originally Posted by Goldfinch View Post
    I'm trying to avoid that,
    That is what is written. Check your brackets to create separate classes, or better yet pull all those classes into their own class files.

  5. The Following User Says Thank You to copeg For This Useful Post:

    Goldfinch (February 1st, 2012)

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: problem with instantiating object

    I found the error on this one. You were right, it was the brackets.

Similar Threads

  1. Help with instantiating a class within a switch statement
    By rawrxeroes in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:40 AM
  2. instantiating a object
    By Neo in forum Object Oriented Programming
    Replies: 7
    Last Post: April 19th, 2011, 02:16 AM
  3. Instantiating jsp's during deployment time
    By tcstcs in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: April 1st, 2011, 02:35 PM
  4. Need help with instantiating a class
    By suxen in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2011, 03:35 PM
  5. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM