Im working on a calculator(yes i am that bored)and im am trying to pass a refrence of the enclosing class to a constructor of another class. The object variable calculator is assigned a refrence to the class in the main method and is declared static. Any ideas why this fails to work? Thanks.
Code:public class Calculator{ static Calculator calculator; public static void main(String[] args){calculator = new Calculator();} }Code:public void addFunctionListeners(JButton[] functions){ for(int i = 0; i < functions.length; ++i){ functions[i].addActionListener(new FunctionListener(calculator)); // passing "this" will not work either } }Code:class FunctionListener implements ActionListener{ Calculator calculator; public void FunctionListener(Calculator calculator){ this.calculator = calculator; } }




and im am trying to pass a refrence of the enclosing class to a constructor of another class. The object variable calculator is assigned a refrence to the class in the main method and is declared static. Any ideas why this fails to work? Thanks.
Reply With Quote