3

In python if I have a method

def hello(a, b):
    print a
    print b

I can call with with either

hello('one', 'two)

or

hello(a='one', b='two')

Can I do a similar thing in java?

The reason I ask is because for unit testing I'm creating an object that looks like

MyObj obj = new MyObj("one", "two", 33, "xxxxxx", 44, 55, "derpity do"); 

and it would be much easier if for reading my code if I could see what each of the arguments relates to.

dwjohnston
  • 11,163
  • 32
  • 99
  • 194
  • 5
    No, you can't but look into the [builder pattern](http://www.javaworld.com/article/2074938/core-java/too-many-parameters-in-java-methods-part-3-builder-pattern.html) – vandale Mar 19 '15 at 05:00
  • Thank you! I guess also I could just add comments specifiying which each arguement is. – dwjohnston Mar 19 '15 at 05:03
  • No, and this is my biggest gripe about Java. – ajb Mar 19 '15 at 05:09

2 Answers2

3

Can I do a similar thing in java?

No, you can't. But, you can use the Builder Pattern.

Community
  • 1
  • 1
Masudul
  • 21,823
  • 5
  • 43
  • 58
1

You can't do that in Java unfortunately. However you can make methods with the same return type as the class itself, and use return this; after setting each field. Using return this; makes method calls chainable with other method calls, and you can then use the method names as "labels" for your parameters. This is quite useful for minimizing redundancy in code, and the most popular variant for it in Java is the Builder Pattern which uses an additional Builder class as a complement to create easily customizable instances of a class. You'll see this type of thing in other languages as well, but most often for other purposes than fancy constructors (e.g. cascading Javascript libraries like JQuery).

For example:

class Car {
    private int year;
    private String make;
    private String model;
    private int mileage;

    public Car year(int year) {
        this.year = year;
        return this;
    }
    public Car make(String make) {
        this.make = make;
        return this;
    }
    public Car model(String model) {
        this.model = model;
        return this;
    }
    public Car mileage(int mileage) {
        this.mileage = mileage;
        return this;
    }
    public void drive(int miles) {
        this.mileage += miles;
    }
}

public class DriveNewCar {
    public static void main(String args[]) {
        Car myDreamCar = new Car()
                                .year(2015)
                                .make("Tesla")
                                .model("Model S")
                                .mileage(0);
        myDreamCar.drive(55);
    }
}

Note that this is not the Builder Pattern, it's just using return this; and short method names to make things a bit clearer on the API-client side, so it's basically just fancy setters.

Unlike Python, Java often likes to be verbose, even for the simplest things. This is why I highly recommend using a proper IDE like IntelliJ, Eclipse, or Netbeans to make your life easier.

Shashank
  • 13,713
  • 5
  • 37
  • 63