title: Java - Override an object created in Constructor?
body: <p>In the following Spring service I am creating <code>ClassToCreate</code>in the constructor of <code>MyService</code>. </p>
  
  <pre><code> @Service("MyService")
   public class MyService {
  
    private final Repository repository;
    private final ClassToCreate classToCreate;
  
          @Autowired
          public MyService(
                  Repository repository,
                  @Value("${path}") String path
                  ) {
  
              this.ClassToCreate = new ClassToCreate(repository, path);
          }
  
          public void myMethod(Object object){
  
          String appendedPath = path + object.id();
  
          //create different instance of classToCreate with variable appended
          ClassToCreate classToCreate = new ClassToCreate(repository, appendedPath);
  
          classToCreate.doSomething();
  
          }
  
  
      }
  </code></pre>
  
  <p>What is the best way to create and use a different instance of <code>ClassToCreate</code> as I am attmempting in <code>myMethod</code> rather than use what is in the constuctor?</p>
  
  <p>I want to create a different instance of the class here using the path from constructor but with the object.id appended as the path for <code>ClassToCreate</code>. I also need to use the same <code>repository</code> value as is passed into the <code>MyService</code> constructor. </p>