Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Passing by reference in Java?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Passing by reference in Java?

    I have a pretty complicated program in which I need to pass by reference. But it seems I can't.

    Rather than post the entire code of my complicated program, I wrote a much simpler program with the same exact problem.

    I want to change the value of the Character object in the function "changechObj7" but it won't change. I keep getting the output 'P' both times.

    How do I fix this? It's so easy in C++.....

    import java.util.Scanner;
     
    public class Play
    {
            public static void main(String[] args)
            {
                    Play play = new Play();
     
                    play.dance();
            }
     
            public void dance()
            {
                    Character chObj7 = new Character('P');
                    System.out.println(chObj7);
                    changechObj7(chObj7);
                    System.out.println(chObj7);
     
            }
     
            public void changechObj7(Character chObj7)
            {
                    chObj7 = 'K';
            }
    }

    I know that the character object is a reference to an address that holds the value "P".

    So, in my "changechObj7" function, I pass the ADDRESS location that holds "P".

    I change what the address holds by setting it equal to 'K'.

    I don't change the address, I change the value the address holds. Shouldn't that be a permanent change?


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    718
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: Passing by reference in Java?

    This does not work this way in java. All you do in your changechObj7 method is to reassign a value to your parameter variable.
    You can still achieve what you want in java, but you have to approach it a little bit differently.
    The fastest and simplest way is to cheat a little and use a one-element char array instead. Then you can change the value at position 0 at the array.
    A cleaner solution would be to define a new class which has a character as public attribute. If you pass an instance of that class you can simply change the value of the attribute.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Passing by reference in Java?

    Cornix provided the best advice if you want to restrict yourself to your design.
    The reason being for this behavior is something called immutability. Here is oracle's brief description: Immutable Objects (The Java™ Tutorials > Essential Classes > Concurrency)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. [SOLVED] Pass-by-Value scheme, puzzled with passing/assigning a reference to another object
    By chronoz13 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 2nd, 2012, 10:43 AM
  2. PrintWriter: passing File reference vs String
    By kahwa in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 12th, 2012, 12:56 AM
  3. What are the 3 types of self-reference in JAVA
    By wholegrain in forum Java Theory & Questions
    Replies: 1
    Last Post: March 5th, 2012, 04:49 PM
  4. Java Poster : Quick Reference guide.
    By SkroefAudio in forum The Cafe
    Replies: 6
    Last Post: September 12th, 2011, 10:56 AM
  5. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 10:57 AM