---
title: "New Object is overriding another Object variable"  
description: "New Object is overriding another Object variable"  
author: "Royce Roy"  
published: 2015-12-27  
updated: 2015-12-27  
canonical: https://www.mindstick.com/forum/33794/new-object-is-overriding-another-object-variable  
category: "java"  
tags: ["java", "oops"]  
reading_time: 2 minutes  

---

# New Object is overriding another Object variable

I have created two [Function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) [objects](https://www.mindstick.com/forum/145447/what-are-objects), the [values](https://www.mindstick.com/forum/327/sum-textbox-values) in the first get overwritten with the values assigned to the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) object.What is [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when) in this approch?\

```
public class Function { private static double coefficient;private static int startX;private static int endX;private static int exponent;protected Function(double coefficient, int startX, int endX, int exponent) {    this.coefficient = coefficient;    this.startX = startX;    this.endX = endX;    this.exponent = exponent;  }      public static void main(String[] args) {    Function func1 = new Function(2, 1, 2, 2);     Function func2 = new Function(0,1,2,1/3);    // now func1 properties are the same as func2      }    }}
```

## Replies

### Reply by Anonymous User

Hey Royce, Modify your code as below:

```
public class Function { private double coefficient;private int startX;private int endX;private int exponent;protected Function(double coefficient, int startX, int endX, int exponent) {    this.coefficient = coefficient;    this.startX = startX;    this.endX = endX;    this.exponent = exponent;  }      public static void main(String[] args) {    Function func1 = new Function(2, 1, 2, 2);     Function func2 = new Function(0,1,2,1/3);    // now func1 properties are the same as func2      }    }}
```

The code you have done , you are making static variables which are only created once for the application. So, every time it overrides the value.


---

Original Source: https://www.mindstick.com/forum/33794/new-object-is-overriding-another-object-variable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
