Skip to main content
added 297 characters in body
Source Link
Zircon
  • 4.7k
  • 17
  • 34

A static variable belongs to the class, not the object, so of course all of your GameBoards are being affected!

Just because you're in a static method doesn't mean you can't manipulate instance variables. First, make everything in your GameBoard class non-static (unless you really do need some of the values shared across all instances). This includes your instance variables and your getter/setter methods.

If your program works exclusively from the main method, then keep your GameBoard[] object static. Then, when you make that method call:

gameboard[input].setState(2); 

This will change only the state of the GameBoard at index input.

Edit:

To instantiate your GameBoard[] with basic GameBoard objects inside of it, you can do this at the beginning of your main method:

for(int x=0; x<gameboard.length; x++) { gameboard[x] = new GameBoard(); //Assuming you want to use the default constructor } 

A static variable belongs to the class, not the object, so of course all of your GameBoards are being affected!

Just because you're in a static method doesn't mean you can't manipulate instance variables. First, make everything in your GameBoard class non-static (unless you really do need some of the values shared across all instances). This includes your instance variables and your getter/setter methods.

If your program works exclusively from the main method, then keep your GameBoard[] object static. Then, when you make that method call:

gameboard[input].setState(2); 

This will change only the state of the GameBoard at index input.

A static variable belongs to the class, not the object, so of course all of your GameBoards are being affected!

Just because you're in a static method doesn't mean you can't manipulate instance variables. First, make everything in your GameBoard class non-static (unless you really do need some of the values shared across all instances). This includes your instance variables and your getter/setter methods.

If your program works exclusively from the main method, then keep your GameBoard[] object static. Then, when you make that method call:

gameboard[input].setState(2); 

This will change only the state of the GameBoard at index input.

Edit:

To instantiate your GameBoard[] with basic GameBoard objects inside of it, you can do this at the beginning of your main method:

for(int x=0; x<gameboard.length; x++) { gameboard[x] = new GameBoard(); //Assuming you want to use the default constructor } 
Source Link
Zircon
  • 4.7k
  • 17
  • 34

A static variable belongs to the class, not the object, so of course all of your GameBoards are being affected!

Just because you're in a static method doesn't mean you can't manipulate instance variables. First, make everything in your GameBoard class non-static (unless you really do need some of the values shared across all instances). This includes your instance variables and your getter/setter methods.

If your program works exclusively from the main method, then keep your GameBoard[] object static. Then, when you make that method call:

gameboard[input].setState(2); 

This will change only the state of the GameBoard at index input.