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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:09:01+00:00 2026-06-09T03:09:01+00:00

Beginner java question, but I cannot understand how call-by-Value ( or Reference ) is

  • 0

Beginner java question, but I cannot understand how call-by-Value ( or Reference ) is working in the example below –

How come the String value is not modified after it exits the method while my custom String Object is. ? Same with other classes like Date..

public class StringMadness {

public static void main(String[] args) {
    String s = "Native String";
    CustomStringObject cs = new CustomStringObject();
    System.out.println("Custom String Before: " + cs.str);
    hello(cs);
    System.out.println("Custom String After: " + cs.str);

    System.out.println("Native String Before: " + s);
    hello(s);
    System.out.println("Native String After: " + s);
}

private static void hello(String t) {
    t = "hello " + t;
}

private static void hello(CustomStringObject o) {
    o.str = "hello " + o.str;
  }
}

class CustomStringObject {

String str = "Custom String";
}
  • 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-09T03:09:03+00:00Added an answer on June 9, 2026 at 3:09 am

    Compare these two methods:

    private static void hello(String t) {
        t = "hello " + t;
    }
    
    private static void hello(CustomStringObject o) {
        o.str = "hello " + o.str;
    }
    

    In the first case, you’re assigning a new value to t. That will have no effect on the calling code – you’re just changing the value of a parameter, and all arguments are passed by value in Java.

    In the second case, you’re assigning a new value to o.str. That’s changing the value of a field within the object that the value of o refers to. The caller will see that change, because the caller still has a reference to that object.

    In short: Java always uses pass by value, but you need to remember that for classes, the value of a variable (or indeed any other expression) is a reference, not an object. You don’t need to use parameter passing to see this:

    Foo foo1 = new Foo();
    Foo foo2 = foo1;
    foo1.someField = "changed";
    System.out.println(foo2.someField) // "changed"
    

    The second line here copies the value of foo1 into foo2 – the two variables refer to the same object, so it doesn’t matter which variable you use to access it.

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

Sidebar

Related Questions

I apologize if this is a really beginner question, but I have not worked
i have a java beginner question: Parent.print() prints hallo in the console, but also
I'm a beginner in Android development but not in programming itself. Anyway, this question
I understand this question couldn't be more beginner, but I am having a hard
probably this is a real beginner question, but i cannot find an answer on
I'll preface my question by saying that I'm a beginner Java EE developer. I
I'm new to Java and I have a beginner question: NumberFormat is an abstract
I am a Java beginner and, I am not really sure what JMSExceptions are
I am a beginner in Java and am working on a Mastermind project for
I understand that the title might not be descriptive enough, but I'm making a

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.