Java Constructors

All instances of Java classes are of type Instance (org.python.core.PyInstance). Here I use a constructor to create a date. In Java, as in Python, a constructor is a method in a class, so this example demonstrates a method returning an instance of class Date.

>>> from java.util import Date
>>> date = Date()
>>> type(date)
<jclass org.python.core.PyInstance at -1204237921>

Constructors don't behave like regular Java methods in Jython, however. Instead, they convert PyInstances of the class even if the class is a Java primitive wrapper. (Remember, methods that return primitive wrappers are converted to the corresponding Python types.) In this context, the Integer and Float constructors return PyInstances.

Import the Java primitive ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.