Help on java assignment

program5.java:18: non-static method newGame(game) cannot be referenced

Page 1 of 1

3 Replies - 1157 Views - Last Post: 02 November 2008 - 10:28 PM Rate Topic: -----

#1 drpppr242  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 08

Help on java assignment

Posted 02 November 2008 - 10:17 PM

I'm working on a java homework assignment and when I try to compile I get this error:
program5.java:18: non-static method newGame(game) cannot be referenced from a static context.
The particular line giving me this error is "goFish = newGame(goFish);" goFish is a game struct. There's more to this program that I don't think is causing the problem, but I guess I wouldn't be posting here if I really knew so please let me know if I should post the whole thing.

*edit: I found out that if I put static after public in the newGame and deckInit functions it works, but I having a feeling that's not the right way to do it.

Main:
public static void main (String args[]) {
		game goFish = new game(); //struct so methods can pass deck & hands
		goFish.gDeck = new bag(52);
		goFish.hand1 = new bag(5);
		goFish.hand2 = new bag(5);
		goFish.hand3 = new bag(5);
		goFish.hand4 = new bag(5);
		
		goFish = newGame(goFish);
		
		System.out.println(goFish.hand1.delete().toString());
		System.out.println(goFish.hand2.delete().toString());
		System.out.println(goFish.gDeck.delete().toString());
		
		} //main



A struct containing the deck and four hands:
class game {
	public bag gDeck;
	public bag hand1;
	public bag hand2;
	public bag hand3;
	public bag hand4;
} //game struct 



The newGame function:
public game newGame(game fish) {
		int i = 0;
		//put all 52 cards in deck
		fish.gDeck = deckInit();
		//Deal four hands
		for(i = 0; i < 5; i++){
			fish.hand1.add(fish.gDeck.delete());
		}
		for(i = 0; i < 5; i++){
			fish.hand2.add(fish.gDeck.delete());
		}
		for(i = 0; i < 5; i++){
			fish.hand3.add(fish.gDeck.delete());
		}
		for(i = 0; i < 5; i++){
			fish.hand4.add(fish.gDeck.delete());
		}
		
		return fish;
	} //newGame


This post has been edited by drpppr242: 02 November 2008 - 10:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help on java assignment

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help on java assignment

Posted 02 November 2008 - 10:19 PM

I believe this will solve your problem.

public static game newGame(game fish)
{
    // ...
}


:D
Was This Post Helpful? 0
  • +
  • -

#3 drpppr242  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 08

Re: Help on java assignment

Posted 02 November 2008 - 10:25 PM

Thanks a lot for the quick response. It did work after adding static. Is there a simple rule to know when to use static?

View PostLocke37, on 2 Nov, 2008 - 10:19 PM, said:

I believe this will solve your problem.

public static game newGame(game fish)
{
    // ...
}


:D

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Help on java assignment

Posted 02 November 2008 - 10:28 PM

Your main() method is static so it can only call static methods

You need an instance of an object to call its non static method like

Rectangle r = new Rectangle();
r.getWidth();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1