Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 790949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:45:24+00:00 2026-05-14T21:45:24+00:00

I have code that looks like this: public class Polynomial { List<Term> term =

  • 0

I have code that looks like this:

public class Polynomial {
    List<Term> term = new LinkedList<Term>();

and it seems that whenever I do something like term.add(anotherTerm), with anotherTerm being… another Term object, it seems anotherTerm is referencing the same thing as what I’ve just inserted into term so that whenever I try to change anotherTerm, term.get(2) (let’s say) get’s changed too.

How can I prevent this from happening?

Since code was requested:

//since I was lazy and didn't want to go through the extra step of Polynomial.term.add
public void insert(Term inserting) {
    term.add(inserting);
}

Code calling the insert method:

poly.insert(anotherTerm);

Code creating the anotherTerm Term:

Term anotherTerm = new Term(3, 7.6); //sets coefficient and power to 3 and 7.6

New code calling the insert method:

poly.insert((Term)anotherTerm.clone());

Which unfortunately still doesn’t work due to clone() has protected access in java.lang.Object, even after doing public class Term implements Cloneable{

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:45:25+00:00Added an answer on May 14, 2026 at 9:45 pm

    OK, replacing my old answer with this, now that I understand the question and behavior better.

    You can do this if you like:

    public void insertTerm(Term term) {
        polynomial.insert(new Term(term));
    }
    

    and then create a new Term constructor like this:

    public Term(Term term) {
        this.coefficient = term.coefficient;
        this.exponent = term.exponent;
    }
    

    That should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From public View(Context context, AttributeSet attrs) constructor docs: Constructor that… May 16, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer You'll get a notification each time the property "selected" is… May 16, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer In a single consolidated database, you will lose some flexibility… May 16, 2026 at 11:22 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have code that looks like this: template<class T> class list { public: class
I have code that looks like this: public class A { public void doStuff()
I have an Address class in C# that looks like this: public class Address
I have code that looks like follows: public interface BaseDAO{ // marker interface }
in vb.net i have some code that looks like this: Imports System Imports System.IO
I'm perplexed by this. I have a login action that looks like this: public
This is my code: public class RegularPolygon { public int VertexCount; public double SideLength;
I have several sections of code that I need to protect with a Mutex.
I have found some similar questions like this one, however there are so many
Okay, I have yet another Code Contracts question. I have a contract on an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.