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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:43:38+00:00 2026-06-18T01:43:38+00:00

class test { public String get() {return s;} private String s=World; } class modifier

  • 0
class test
{
  public String get() {return s;}
  private String s="World";
}

class modifier
{
   public static void modify(ref String v)
   {
     v+="_test";
   }
}

String s1="Earth";


modifier.modify(ref s1); <-------- OK

test c=new test();
modifier.modify(ref c.get()); <------- Error

How to pass in the “modifier” string returned by the function?
Assignment through another String object is unacceptable. So how will the copy is created.

  • 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-06-18T01:43:40+00:00Added an answer on June 18, 2026 at 1:43 am

    If you dont like Matthew Watson’s answer and you want to stay to your approach, the way to go is by using StringBuilder.That is a String that can change its value without creating a new one!

    What i mean:
    Normal String’s value cant be changed, what is happening is that a new String is being created….and the pointer(that points to the String you want to change its value) points from now on to this new String. All other pointers….that “were pointing” to the old String …..are still pointing…to the old String!(the String’s value didnt change!)
    I am not sure if thats clear enough but you have to understand this if you want to play with Strings.This is exactly why s1 cant change its value.

    The workaround is with StringBuilder:

        class test
    {
        public StringBuilder get() { return  s; }
        private StringBuilder s = new StringBuilder("World");
    }
    
    class modifier
    {
        public static void modify(StringBuilder v)
        {
            v.Append("_test");
        }
    }
    

    And some test code : (of course all this comes with processing cost…but i dont think that will be an issue for now)

            StringBuilder s1 = new StringBuilder("Earth");
    
            System.Diagnostics.Debug.WriteLine("earth is {0}", s1);
            modifier.modify(s1); //<-------- OK
            System.Diagnostics.Debug.WriteLine("earth is {0}",s1);
    
            test c=new test();
            StringBuilder aa=c.get();
            System.Diagnostics.Debug.WriteLine("earth is {0}", aa);
            modifier.modify(aa); //<------- Error(not anymore)
            System.Diagnostics.Debug.WriteLine("earth is {0}", c.get());
    

    Before you use the code try to understand how String and StringBuilder works

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

Sidebar

Related Questions

class Test { public static void main(String[] args) throws Exception { Test t =
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
Take a look at this code: public class Test { public static void main(String...
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: class Test { public static void main(String[] args) {
I am compiling a simple program class Test { public static void main(String[] args)
import javax.swing.*; public class test { public static void main(String[] args) throws Exception {
import org.joda.time.LocalDate; public class Test { public static void main(String[] args) { long time=System.currentTimeMillis();
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
How bad is something like: public class Test { private string pKey = null;

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.