# Converting Ruby objects to Java using to\_java

**URL:** <https://www.ruby-forum.com/t/converting-ruby-objects-to-java-using-to-java/156552>\
**Category:** JRuby\
**Created:** [December 29, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/converting-ruby-objects-to-java-using-to-java/156552 "2008-12-29T23:45:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/76d3ee/32.png) [@pran](https://www.ruby-forum.com/u/pran)\
**Post date:** [December 29, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/converting-ruby-objects-to-java-using-to-java/156552/1 "2008-12-29T23:45:28Z")

</div>

Hi, I’m new to JRuby and I have a question regarding passing ruby  
variables to java.

Situation:  
In java, I have an abstract class, called JClassA and another class  
called JClassB.  
In ruby, I implement the concreate class of JClassA, called RClass.

Here are the relations:

RCLass \< JClassA.  
JClassB(JClassA) (The constructor of JClassB takes JClassA as an  
argument)

Problem:  
From RClass, I need to call JClassB and give the current instance as an  
argument.

If I do JClassB.new(self), then it will complain about not finding the  
constructor.

On the other hand, if I do  
JClassB.new(self.to\_java(Java::SomePackage.JClassA)) then it complains  
that it cannot find the method to\_java in RClassB.

What am I doing wrong? Can I convert any ruby object to java object?

Thanks,  
Pran.

---

<div class="post-metadata">

**Author:** ![pran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/76d3ee/32.png) [@pran](https://www.ruby-forum.com/u/pran)\
**Post date:** [December 30, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/converting-ruby-objects-to-java-using-to-java/156552/2 "2008-12-30T17:50:48Z")

</div>

Figured it out.

You cannot send a reference of ‘self’ from the ‘initialize’ method.
