Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating a non-static Java Inner Class from JRuby

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?

like image 707
rampion Avatar asked Feb 04 '10 13:02

rampion


2 Answers

i = Outer::Inner.new(o,2)
like image 112
sepp2k Avatar answered Sep 18 '22 05:09

sepp2k


From what can be seen in this discussion, you'll have to do Outer:Inner.new(o, 2)

like image 39
Valentin Rocher Avatar answered Sep 19 '22 05:09

Valentin Rocher