Results 1 to 4 of 4

Thread: Passing object refrences?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Passing object refrences?

    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; 
      }
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What's the exact error?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Ah i see it already. I do this alot and i should get out of the habit. The signature of the constructor is marked with void so the compiler treats it as a method.

  4. #4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width