Can I reset the arguments that are sent through the parameters, from inside the method itself?
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.
Can I reset the arguments that are sent through the parameters, from inside the method itself?
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.
FightingIrishman (January 2nd, 2022)
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 😁
FightingIrishman (January 2nd, 2022)
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.
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.
The output from this ispublic 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; } } }
Here some good explanations of how it works.12 15 15
I found Dan Carters comment on this quite apt:
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.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.