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

Thread: Are method parameters two way traffic?

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Are method parameters two way traffic?

    Can I reset the arguments that are sent through the parameters, from inside the method itself?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Are method parameters two way traffic?

    Arguments are sent as values to the method. The method does not have access to the variable that the arguments came from.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    FightingIrishman (January 2nd, 2022)

  4. #3
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Are method parameters two way traffic?

    Quote Originally Posted by FightingIrishman View Post
    Can I reset the arguments that are sent through the parameters, from inside the method itself?
    No, as explained by Norm. And even if you could, it would be very bad design. Nobody expects a method to have side effects in code outside the method, let alone mess with variables defined on a higher level.
    If you REALLY want to do stuff like this, there's always C 😁

  5. The Following User Says Thank You to cbreemer For This Useful Post:

    FightingIrishman (January 2nd, 2022)

  6. #4
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Are method parameters two way traffic?

    Is it true that with instance/non-static/object methods, parameters are two way traffic?

    Pardon me. I think I've realised that the answer is no.

    An object of an instance method can set the instance variables of the method. I was confusing this to mean the other direction.

  7. #5
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Thumbs down Re: Are method parameters two way traffic?

    Although Java parameters are defined as "passed by value" it doesn't hold quite so fast with objects as it does with simple types.
    If an object is passed as a parameter, that parameter is copied (satisfying pass by value) however that copy is still pointing to the original instance.
    Therefore you can call the original instances methods and manipulate the fields of that original instance.
    Easier to show than to explain.
    public class Main
    {
    	public static void main(String[] args) {
    	    MyClass test = new MyClass(); 
    	    test.setN(12);
    	    System.out.println(test.getN());
    	    changeN(test); 
    	    System.out.println(test.getN());	
    	    dontChangeN(test);
    	    System.out.println(test.getN());	
    	}
    	private static void changeN(MyClass mc){
    	    mc.setN(15); 
    	}
    	private static void dontChangeN(MyClass mc){
    	    MyClass newMC = new MyClass(); 
    	    newMC.setN(16); 
    	}
     
    	public static class MyClass {
    	    private int n; 
    	    private void setN(int value){
    	        this.n = value; 
    	    }
    	    private int getN(){
    	        return this.n; 
    	    }
    	}
     
    }
    The output from this is
    12
    15
    15
    Here some good explanations of how it works.
    I found Dan Carters comment on this quite apt:
    Java always passes arguments by value, but what you are passing by value is a reference to an object, not a copy of the object.
    In some other languages (like c# and VBA) when passing by value there is an actual copy made and the function has no access to the original object instances methods.

Similar Threads

  1. java program taking parameters inside a method
    By prof.chemuturi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 7th, 2013, 10:39 PM
  2. Traffic Simulator
    By slimergan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2012, 06:38 AM
  3. [SOLVED] Why re-name these method parameters?
    By pict3000 in forum Object Oriented Programming
    Replies: 6
    Last Post: August 9th, 2012, 06:07 AM
  4. Calling method cant get the parameters right
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 22nd, 2011, 02:23 PM
  5. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM