Search code examples
javajythonjython-2.7

Java/Jython Hypothesis: constructor should be public


I am not a Java programmer, but I have very good knowledge of OOP in C++, Scala and Python, so I will understand abstract explanations.

Im working with some java code like this:

public class my_class implements ObjectReader<double[][]>, ObjectWriter<double[][]> {
    private static final my_class instance = new my_class();
    private my_class() {};
    public static my_class getInstance(){
        return instance
    }
    public double[][] functionName{String argument) {
        // this is the function I want to use, but it is giving me some errors.
    }
... MORE CODE AND BRACKETS AND SYNTACTICALLY CORRECT STUFF ...

ObjectReader and ObjectWriter are defined in this library as:

public interface ObjectReader<T>{
    T functionName(String argument);
}

Then in jython 2.7 I want to use this code (PROPER IMPORTS DONE AND WHATHAVEYOU)

my_class_instance  = path.to.package.my_class()
data_i_want = my_class_instance.functionName("A String")

And then there is the error

TypeError: No visible constructors for class path.to.package.my_class)

a) I think in java if you specify a constructor in the class body, it is an overridden constructor. The 'principal' constructor would be given like:

public class my_class(type1 arg1, type2 arg 2, ...){
...
}

so does my_class require arguments to the constructor or not?

b) I tried deleting the parentheses from the jython code like this:

my_class_instance  = path.to.package.my_class // No more () here!
data_i_want = my_class_instance = my_class_instance.functionName("A String")    

and get the error:

TypeError: functionName() expected 2 args; got 1

I see alot of posts online about these two issues, but if anyone knows the answer to this riddle off the top of their head, it would be a great time saver. It's 2 AM and I want to sleep!


Solution

  • This produces an error since there are no visible constructors of my_class:

    my_class_instance = path.to.package.my_class()
    

    This does not work since it gives you a reference to the class, not an instance:

    my_class_instance = path.to.package.my_class
    

    But you have a factory method called getInstance(). So use that to get an instance:

    my_class_instance = path.to.package.my_class.getInstance()