jython and toString


For one java class (Hello) i use another (HelloPrinter) to build the
string representation of the first one. When i've tried to use this
from within jython, HelloPrinter.toString(hello) call gives results
like Object.toString() of hello has being called. The example below
shows this behaviour.
Could somebody explain this?

// Hello.java
package jythontest;
public class Hello {
private String name;
public Hello(String name)
this.name = name;
public String sayHello()
return "Hello, "+name;

// HelloPrinter.java
package jythontest;
public class HelloPrinter {
public static String toString(Hello h)
return h.sayHello();

public static String toMyString(Hello h)
return h.sayHello();

# calljava.py
from jythontest import *
h = Hello("theName")
print h
print HelloPrinter.toString(h)
print HelloPrinter.toMyString(h)

jythontest.Hello@523ca2 // GOOD
jythontest.Hello@523ca2 // WRONG
Hello, theName // GOOD

Jython 2.1 on java (JIT: null)

java version "1.5.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
Java HotSpot(TM) Server VM (build 1.5.0_03-b07, mixed mode)


Relevant Pages

  • Re: jython and toString
    ... For one java class i use another to build the ... string representation of the first one. ... package jythontest; ... but I'm guessing toString isn't meant ...
  • Re: Help me!! Why java is so popular
    ... String or StringBuffer? ... keep in mind I knew NOTHING about Java. ... The language accepted it, no warnings, and thus I ... in some native assemblers you can peruse the instruction ...
  • Re: question about assigning null to a reference object
    ... String getAuthor() ... void setAuthor ... b.setTitle("Thinking in Java 4th Edition"); ...
  • Re: Java questions: Urgent
    ... why I would expect almost any Java programmer to be able to answer ... Which is the main reason I "butted" in a bit.. ... on the characters in the String. ... > therefore naturally cover all the basics, or they have missed out badly ...
  • MD5 solved differently, in an openplatform manner ... (Re: Problems with a DLL in ooRexx 4.0.0 under
    ... Rather than going for a particular Rexx function package, you could do something else: ... advantage of Java that is probably installed on your machine. ... returned Java byte array into a hexadecimal string with the help of Rexx. ... rxfuncquery for name: rxwibble was: 1 ...