Bazinga
Bazinga

Reputation: 2466

Instantiating object in Python same as Java using jython

In Java I was able to run my code as: (This are just sample naming)

import com.projectname.api.APIOne;
import com.projectname.api.APITwo;
import com.projectname.api.APIThree;
import com.projectname.api.APIFour;

import com.projectname.api.MainAPI;

public class TestMain {

    public static void main(String[] args) {

        APIOne a = APIOne.getName();
        APITwo b = APIThree.getAddress();
        APIFour d = b.getEmail();

        MainAPI mainapi = new MainAPI();
        mainapi.setEmail(d)
    }
}

It is running okay, I tried converting this to Python as:

import com.projectname.api.APIOne as APIOne;
import com.projectname.api.APITwo as APITwo;
import com.projectname.api.APIThree as APIThree;
import com.projectname.api.APIFour as APIFour;

def test():

    a = APIOne.getName();
    b = APIThree.getAddress();
    d = b.getEmail();

    mainapi = MainAPI();
    mainapi.setEmail(d)

test()

But is this the right way of instantiating? It make me confuse on instantiating.

Hope you could help me.

Upvotes: 0

Views: 137

Answers (2)

Dunes
Dunes

Reputation: 40703

Importing a class from a java package or python module is normally written as:

from java.lang import Math

Rather than:

import java.lang.Math as Math

But, your code is correct.

Upvotes: 1

enrico.bacis
enrico.bacis

Reputation: 31494

I don't understand why you are confused, but this is correct, you could check the Jython documentation about instantiating Java objects using Jython and instantiates the objects the same way as you do.

Upvotes: 1

Related Questions