Instantiating a non-static Java Inner Class from JRuby
-
18-09-2019 - |
Question
So given the following java class:
class Outer
{
private int x;
public Outer(int x) { this.x = x; }
public class Inner
{
private int y;
public Inner(int y) { this.y = y; }
public int sum() { return x + y; }
}
}
I can create an instance of the inner class from Java in the following manner:
Outer o = new Outer(1);
Outer.Inner i = o.new Inner(2);
However, I can't seem how to do the same from JRuby
#!/usr/bin/env jruby
require 'java'
java_import 'Outer'
o = Outer.new(1);
i = o.Inner.new(2); #=> NoMethodError: undefined method `Inner' for #<Outer...>
What's the correct way to do this?
Solution
i = Outer::Inner.new(o,2)
OTHER TIPS
From what can be seen in this discussion, you'll have to do Outer:Inner.new(o, 2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow