Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Static methods and passing functions as parameters.

Reply
Thread Tools

Static methods and passing functions as parameters.

 
 
JBB123 JBB123 is offline
Junior Member
Join Date: Dec 2011
Posts: 2
 
      12-06-2011
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
 
Reply With Quote
 
 
 
 
JBB123 JBB123 is offline
Junior Member
Join Date: Dec 2011
Posts: 2
 
      12-08-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with Static and non-static methods and feilds o_ssie Java 0 04-09-2009 04:38 PM
Re: Python callback functions and static methods Steve Holden Python 0 01-05-2009 06:07 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
static member functions access to class members and methods Roger Leigh C++ 11 01-22-2004 12:49 PM



Advertisments