jeudi 14 juillet 2016

Java constructor usage in different classes

I'm using a constructor to init a object in a different class than the constructor itself. The constructor gets a string and returns an object instance. But how do I get the string from the class creating the object to the class where the constructor is defined? My project is like this:

The class with the constructor:

public class UriParserImplementation implements UriParser {

@Override
public Uri parse() {    
    return null;
}
public UriParserImplementation(String uri) {
}
}

This is the class using the constructor with a string to create an object instance:

public final class UriParserFactory {
    public static UriParser create(String uri) {
        UriParser parser = new UriParserImplementation(uri);
        return parser;
    }
}

And I wanted to use the string used by UriParserFactory in UriParserImplementation but I don't know how to do it.

Aucun commentaire:

Enregistrer un commentaire