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

  • Home
  • SEARCH
  • 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 6996641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:11:01+00:00 2026-05-27T20:11:01+00:00

I have a class (Literal). I need to be able to keep an intact

  • 0

I have a class (Literal). I need to be able to keep an intact Literal instance in memory throughout my application, and have a copy of it which I can alter. I have used two ways to do this:

  1. class Literal implements Cloneable and override Object.clone() method.
  2. Factory constructor:

    public Literal(Literal lit){
         this = lit;
    }
    

In both cases copying doesn’t work. Every change I make to the copy, changes the original. Does anybody have any idea what I’m doing wrong?

  • 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-27T20:11:02+00:00Added an answer on May 27, 2026 at 8:11 pm

    If you are going to use copy-constructor, then you will need to make a deep copy of every mutable member variable.

    Say your Literal has member variables like this:

    private String[] args;
    private String s;
    

    Then your copy-constructor would need to do something like this

    public Literal(Literal l) {
      // have to deep copy the array, otherwise both instances are sharing the reference
      this.args = Arrays.copyOf(l.args, l.args.length);
    
      // safe to just copy the reference, String is immutable
      this.s = l.s;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Literal class that defines a text value and an ID. I
I'm using literal to implement css, so as to have the css class vary
I have class method that returns a list of employees that I can iterate
I have class A, which exposes an event. an object of class B subscribed
I want my User Control to be able to have Literal Content inside of
suppose we have a model in django defined as follows: class Literal: name =
I have an abstract test class that has two type parameters. I need to
So if I have a class like: CustomVal I want to be able to
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class

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.