Static methods and passing functions as parameters.

Joined
Dec 6, 2011
Messages
2
Reaction score
0
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
 
Joined
Dec 6, 2011
Messages
2
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top