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 4 of 4

Thread: Cannot pass variables?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Cannot pass variables?

    in my instance field I have variables a = 1 and b = 2. I can not seem to pass "a" and "b" through my swap method as a parameter. I am confused to why.

            public void swap(int i1, int i2)
        {
            int tmp = i1;
            i1 = i2;
            i2 = tmp;
            System.out.println("i1 -  " + i1);
            System.out.println("i2  -  " + i2);
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Cannot pass variables?

    Java passes the values contained in the variables which the method receives in new temporary variables.
    There is no way to change the contents of the original variables.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cannot pass variables?

    Oh, so to make the swap method work I would have to create public variable which is bad design practice?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Cannot pass variables?

    If you put the values in an array you would be able to swap them
    It is not often that you swap the values of class variables. There wouldn't be a need for a method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 07:34 PM
  2. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  3. 'pass by value' and 'pass by reference'
    By pokuri in forum Object Oriented Programming
    Replies: 5
    Last Post: January 26th, 2011, 10:30 AM
  4. how to pass a variable from a method to another
    By amr in forum Java Theory & Questions
    Replies: 1
    Last Post: November 20th, 2010, 09:32 PM
  5. Can't pass to the other method
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2010, 09:44 AM