I have two Java classes:
BoardManager & Board
BoardManager is the main class. Inside it contains a board:
public BoardManager(){
Board b = new Board();
}
public methodToBeCalled(){}
public static void main(String[] args) {
new BoardManager();
}
Board is the user interface. When a user presses a button on the interface I want it to then call a method in the BoardManager class, but that obviously presents a problem and I am unsure how to get around it.
One solution was to move the main method into Board and so this:
BoardManager boardManager;
public Board(){}
public void buttonPressed(){
boardManager.methodToCall();
}
public static void main(String[] args) {
boardManager = new BoardManager();
}
But that just throw up errors about static and unstatic etc.
Solutions? Thanks!
Either this:
class Board{
static BoardManager boardManager;
public Board(){}
public void buttonPressed(){
boardManager.methodToCall();
}
public static void main(String[] args) {
boardManager = new BoardManager();
}
}
Or this:
class Board {
BoardManager boardManager;
public Board(){ }
public void buttonPressed(){
boardManager.methodToCall();
}
public static void main(String[] args) {
Board b = new Board();
b.boardManager = new BoardManager();
}
}
You can't call a non-static field(BoardManager) from a static method (main). Read more about the static keyword.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.