java - Calling a method in main class from non main class -
i have 2 java classes:
boardmanager & board
boardmanager main class. inside contains board:
public boardmanager(){ board b = new board(); } public methodtobecalled(){} public static void main(string[] args) { new boardmanager(); }
board user interface. when user presses button on interface want call method in boardmanager class, presents problem , unsure how around it.
one solution move main method board , this:
boardmanager boardmanager; public board(){} public void buttonpressed(){ boardmanager.methodtocall(); } public static void main(string[] args) { boardmanager = new boardmanager(); }
but throw errors static , 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 non-static field(boardmanager) static method (main). read more static keyword.
Comments
Post a Comment