Home (Java) Can I create an object and use a setter on the same line?
Reply: 2

(Java) Can I create an object and use a setter on the same line?

EasyNewUser33
1#
EasyNewUser33 Published in 2018-01-11 12:25:34Z

Is it possible to create an object in JAVA and then instantly use a setter on the same line?

Like this: Class classname = new Class().setSomething("String");

How can I do it correctly, eclipse always says :

Type mismatch: cannot convert from void to type Class

Lino
2#
Lino Reply to 2018-01-11 13:27:55Z

The error you're getting is just because the return value of setSomething() is void. E.g. the method looks like this;

public void setSomething(String something){
    this.something = something;
}

And because void is not assignable to anything you can't create your object inline and call a method on it. But it still is possible when changing the signature to something like this:

class MyClass{
    private String something;

    public MyClass setSomething(String something){
        this.something = something;
        return this; // returning this
    }
}

This use of return this is called method-chaining and can be seen mostly in the builder pattern.

Whilst the above may work you probably want to always set something to a value when creating an instance of MyClass. To do this, you have to add a custom constructor which accepts an initial value for something. E.g:

class MyClass{

    private final String something;

    public MyClass(String something){
        this.something = something;
    }
}

Which then can be used like the following:

MyClass myclass = new MyClass("Something");

The latter is prefered over the prior. Because that way the object is created with the value set and can not be in an invalid state. (E.g. when no something is set, which may lead to nasty NullPointerExceptions).

Note: You may have seen that i used the keyword final when declaring the variable something. This may be omitted if you want to be able to change something later to a different value, but when you're leaving it as is then you've created an immutable MyClass instance which is mostly prefered over mutable instances.

Odilon
3#
Odilon Reply to 2018-01-11 12:53:45Z

You can do something like it using Builder Pattern as @Jens said.

public class Car {

   private String brand;

   public Car(String brand) {
      this.brand = brand;
   }

   public static class Builder {

      private String brand;

      public static Builder withBrand(String newBrand) {
         Builder builder = new Builder();
         builder.brand = newBrand;        
         return builder;
      }

      public static Car build() {
         return new Car(this.brand);
      }
   }
}

So you can call a new car this way:

Car evoque = new Car.Builder().withBrand("Range Rover").build();

Builder Pattern is very useful when you have many atributes to set and dont want an extensive constructor.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302041 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO