Results 1 to 3 of 3
  1. #1
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default Static method cannot make new objects?

    I have this line of code in my deck class..

    Java Code:
    public static void createDeckBlackJack(){
    		Card[] deck1 = new Card[24];
    		String suit = "";
    		int count = 0;
    		int n = 0;
    		while (count < 2){ 
    			switch(count){
    			case 0:
    			suit = "Hearts";
    			case 1:
    			suit = "Diamonds";
    			case 2:
    			suit = "Clubs";
    			case 3:
    			suit = "Spades";
    			}
    			deck1[0 + n] = new Card(2, "2", suit, 0);
    			deck1[1 + n] = new Card(3, "3", suit, 0);
    			deck1[2 + n] = new Card(4, "4", suit, 0);
    			deck1[3 + n] = new Card(5, "5", suit, 0);
    			deck1[4 + n] = new Card(6, "6", suit, 0);
    			deck1[5 + n] = new Card(7, "7", suit, 0);
    			deck1[6 + n] = new Card(8, "8", suit, 0);
    			deck1[7 + n] = new Card(9, "9", suit, 0);
    			deck1[8 + n] = new Card(10, "Jack", suit, 0);
    			deck1[9 + n] = new Card(10, "Queen", suit, 0);
    			deck1[10 + n] = new Card(2, "King", suit, 0);
    			deck1[11 + n] = new Card(11, "Ace", suit, 1);
    			n += 12;
    			count++;
    			}
    		}
    I have another class that has my "main" in it. I put createDeckBlackJack();, and then tried to type deck1. in eclipse.. No options.. just a red underline.. Why is it not recognizing this object that was created by the method?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This has nothing to do with the method being static and everything to do with deck1 being local to this method and is thus invisible outside of it.

  3. #3
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This has nothing to do with the method being static and everything to do with deck1 being local to this method and is thus invisible outside of it.
    Would I have to change the object to static as well then?

    errr. dur.. I think i just figured out what to do. Make the method return a Card[] array.

Similar Threads

  1. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 05:05 AM
  2. make static ref to non-static method?
    By McChill in forum New To Java
    Replies: 7
    Last Post: 02-23-2009, 06:48 AM
  3. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  4. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 09:51 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 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
  •