How do I create a object in a static constant helper class with void set methods?

Viewed 594

So I have object

@Data
public class Bicycle{
     private String color;
     private long speed;
     private String tag;

    public Bicycle(String color, long speed){
         color = color;
         speed = speed;   
    }

    public void setTag(String tag){
        tag = tag;
    }
}

I also have a Helper class that contains all my constants where I store a Bicycle I want to reference alot. I want to add a Bicylce with color="blue",speed=5L,tag="mountain" as a public static variable but I'm not sure how to do that since the constructor doesnt use tag, and setTag returns void. I don't own this Bicycle class so I can't add it to the constructor.


public class Helper{
   public static final Bicycle = new Bicycle("blue",5L);

}

Because of this, whenever I have to create this bicycle


     main{
         Bicycle bicycle = Helper.Bicycle;
         Bicycle.setTag"mountain");
   }

How would I create this bicycle with the "mountain" tag in the Helper class?

6 Answers

Add a static block in your Helper like:

public class Helper {
    public static final Bicycle bicycle = new Bicycle("blue", 5L);
    static {
        bicycle.setTag("mountain");
    }
}

And fix the assignments like tag = tag -> this.tag = tag.

You can add a static block into your helper class to do more stuff you cannot do when you instantiate your object.

public class Helper {
   public static final Bicycle bicycle = new Bicycle("Blue", 5L);

   static {
      bicycle.setTag("mountain");
   }
}

Its returning void because you aren't really assigning properly

    @Data
public class Bicycle{
     private String color;
     private long speed;
     private String tag;

    public Bicycle(String color, long speed){
         this.color = color;
         this.speed = speed;   
    }

    public void setTag(String tag){
        this.tag = tag;
    }
}

your helper class

     public class Helper{
       public static final Bicycle bicycle = new Bicycle("blue",5L);
         static{
           bicycle.setTag("mountain");
}
    
    }

The pattern I would suggest you use is the factory pattern. It's like what you're doing, but your helper class would expose a method rather than a static variable. So you might have:

public class Helper{
    private static Bicycle bicycle = null;
    public static Bicycle getBlueMountainBike() {
        if (bicycle == null) {
            bicycle = new Bicycle("blue", 5L);
            bicycle.setTag"mountain");
        }
        return bicycle;
    }
}

main{
     Bicycle bicycle = Helper.getBlueMountainBike();
}

I very much like the other answers that add the static block to solve the problem while keeping the original API.

The factory method approach does have one advantage that could be significant in specific applications. Say you've got a helper class like this with 100 such constants, say many types of vehicles, bikes and other things. And say that any particular application of the helper class uses just a few of the many constants in the helper. With the factory approach, you save the time and space of constructing all of those other objects that will never be used.

I'd probably create a static factory function:

public class Helper{
   public static final blueMountainbike = createBicycle("blue", 5L, "mountain");

   public static Bicycle createBicycle(String color, long speed, String tag) {
       Bicycle bicycle = new Bicycle(color, speed);
       bicycle.setTag(tag);
       return bicycle;
   }
}

This has the advantage that it can be reused to create different bicycles.


Side note: try to come up with a better name for Helper. If the main puprose is creating and accessing instances of bicyles, BicycleFactory might be appropriate.

this return void because you aren't assigning properly. your helper class would expose a method rather than a static variable

    @Data
public class Bicycle{
     private String color;
     private long speed;
     private String tag;

    public Bicycle(String color, long speed){
         this.color = color;
         this.speed = speed;   
    }

    public void setTag(String tag){
        this.tag = tag;
    }
}

your helper class

     public class Helper{
       public static final Bicycle bicycle = new Bicycle("blue",5L);
         static{
           bicycle.setTag("mountain");
}
    
    }

read this if you are beginner use this. in java

Related