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 6064189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:15:11+00:00 2026-05-23T09:15:11+00:00

I have a class SBContainer which has a StringBuffer member mySB . I am

  • 0

I have a class SBContainer which has a StringBuffer member mySB. I am implementing Cloneable for SBContainer like this –

SBContainer implements Cloneable {
    public StringBuffer mySB;
    public SBContainer() {
        mySB = new StringBuffer("This is a test string");
    }
    public Object clone() throws CloneNotSupportedException {
        SBContainer cloned = (SBContainer)super.clone();
        return cloned;
    }
}

Now, I create an object sbc1 for MyContainer and it’s clone sbc2. It looks like the container mySB is not cloned; and sbc1.mySB and sbc2.mySB are pointing to the same StringBuffer object. I tested using the following class –

public class SBContainerTest {
    public static void main(String[] args) {
        SBContainer sbc1 = new SBContainer();
        SBContainer sbc2 = null;
        try {
            sbc2 = (SBContainer)sbc1.clone();
        } catch(CloneNotSupportedException e) {
            e.printStackTrace();
        }
        sbc1.mySB.append(" ...something appended");
        System.out.println(sbc2.mySB);
    }
}

EDIT:
The output was: This is a test string ...something appended

So, I tried to clone mySB like this –

cloned.mySB = (StringBuffer)mySB.clone();

…and I get this error –

SBContainerTest.java:8: clone() has protected access in java.lang.Object
        cloned.mySB = (StringBuffer)mySB.clone();
                                        ^
1 error

So, how can I achieve this? How can I clone a mutable member of a class which implements Cloneable?

Thanks.

  • 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-23T09:15:12+00:00Added an answer on May 23, 2026 at 9:15 am

    StringBuffer is not Cloneable, so you’ll have to clone it manually. I suggest something like this in your clone method:

    public Object clone() throws CloneNotSupportedException {
        SBContainer cloned = (SBContainer)super.clone();
        cloned.mySB = new StringBuffer(this.mySB);
        return cloned;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class Employee which is something like this. class Emp { int EmpID;
I have class: public class EnglishWord implements Serializable, Comparable, Cloneable { static Logger logger
I have class like this: public class object { [Key] int number; String mystring;
I have class like public class ProgressBars { public ProgressBars() { } private Int32
I have class foo that contains a std::auto_ptr member that I would like to
i have class like this DrillDownAppAppDelegate.h PictureCell.h RootViewController.h SlideShowViewController.h DrillDownAppAppDelegate.m PictureCell.m RootViewController.m SlideShowViewController.m i
I have class Lazy which lazily evaluates an expression: public sealed class Lazy<T> {
I have class Country which has collections of Cities. At client I use webmethod
I have class like following: public class Tree<T extends Comparable<? super T>> { //
I have class which has a method that needs to return three DataTables. I

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.