Static methods and passing functions as parameters.

Discussion in 'Java' started by JBB123, Dec 6, 2011.

  1. JBB123

    JBB123

    Joined:
    Dec 6, 2011
    Messages:
    2
    Hi,

    I'm developing a mathemathical model which needs to apply a certain method to many different functions.
    To acomplish this I'm using an Interface, which I have read is the most common procedure to pass a functions as a parameters.

    /**
    * Define a common interface to be able to calculate the Residues of different functions
    */
    public interface ComplexFunction {
    Complex value(Complex k, double m);
    }

    /**
    * Define the function
    */
    private class A2Complex implements ComplexFunction {
    public Complex value(Complex k, double m) {
    .....
    }
    /**
    * Pass the function
    */
    private double A20(double k0, double m) {
    ComplexFunction A2C = new A2Complex();
    return Residue(new ResidueFunction(A2C, k0, m));
    }

    So far, so good.
    But the problem arises when I try to use this code:

    System.out.println(String.valueOf( A20(5.0,1.0) );

    The compiler tells me to declare A20 as static, but when I do so, I get an error in the "new A2Complex();" line saying "No enclosing instance is accesible"

    I'm clearly not understanding properly the implications of making a method Static.

    Could somebody please help me understanding this?

    Thanks in advance,

    JBB
     
    JBB123, Dec 6, 2011
    #1
    1. Advertising

  2. JBB123

    JBB123

    Joined:
    Dec 6, 2011
    Messages:
    2
    I still don't understand clearly why it gave me the error "No enclosing instance is accesible", but I have worked around it and solved it by joining the ResidueFunction and A2Complex in one:

    private static class A2ComplexR implements UnivariateRealFunction {
    }
    private static double A20(double k0, double m) {
    UnivariateRealFunction A2C = new A2ComplexR(k0, m);
    return Residue(A2C);
    }

    now they can all be Static with no problem.

    JBB
     
    JBB123, Dec 8, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,518
    Tony Morris
    Oct 19, 2004
  2. Roger Leigh
    Replies:
    11
    Views:
    3,430
    Jeff Schwab
    Jan 22, 2004
  3. Steve Holden
    Replies:
    0
    Views:
    666
    Steve Holden
    Jan 5, 2009
  4. o_ssie
    Replies:
    0
    Views:
    336
    o_ssie
    Apr 9, 2009
  5. Kenneth McDonald
    Replies:
    5
    Views:
    376
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page