• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

pass by reference and value

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


changes in the reset method affects the Point object becoz objects are passed by reference.This i understood.But is the value of variable a remaining the same becoz increment is a static method and int a is not static???or if we try to print x instead of a ,can we get the updated result???
Can anyone please explain it.

[edit]Add code tags. CR[/edit]
[ September 02, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The behavior has nothing to do with static. The methods within your class have to be static, since they are called directly from the main() method and thus from a static context. If you pass a primitive data type as int as method argument, you actually get a new variable with the same value assigned within the method. Therefore, variable changes affect only the local variable instance in your method. The changes applied to the local method variable will simply get lost.

Regards,
Thomas
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Point{
public int x;
public int y;
}
public class ReferencePassingTest{
public static void increment(int x){
x++;
}
public static void reset(Point point){
point.x=0;
point.y=0;
}
public static void main(String[] args){
int a=9;
increment(a);
System.out.println(a);//prints same 9
Point p=new Point();
p.x=400;
p.y=600;
reset(p);
System.out.println(p.x);//print changed value 0
}
}

For your first q. answer is yes as static methods have higher priority and you cannot make variable 'a' static as you are passing a non static variable in your increment method..i did not understand the second part of your q.
 
sivasubramanian nagarajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your first q. answer is yes as static methods have higher priority and you cannot make variable 'a' static as you are passing a non static variable in your increment method..i did not understand the second part of your q. sorry for posting the 1st reply with the entire code was using it for reference but added it by mistake
 
Marshal
Posts: 80091
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by geo yo:
. . . objects are passed by reference.

No they aren't. There is a very easy rule in Java to remember what is passed by reference . . .





Nothing.


There is no such thing as pass-by-reference in Java. Everything is passed by value. If you do a search in the Ranch you find old threads like this one where other people got confused about pass by value and pass by reference. Please look at that thread and its included links.
But to repeat: there is no such thing as pass by reference in Java.

When you look through that old thread and its links you find there are some languages where there is true pass-by-reference; you can alter the contents of the reference passed. In the case of Java, when passing objects, you cannot change the reference passed, at all. The object in that reference will always be the object in that reference and a called method cannot change it however hard it tries.
But if use the value passed to gain access to a mutable object you can call methods on that object which change its state. That is what you are actually doing.

And I don't understand the bit about static methods having higher priority; it doesn't sound correct to me, I am afraid.
 
Campbell Ritchie
Marshal
Posts: 80091
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by geo yo:
... becoz objects are passed by reference.


To add to what Campbell already said about this:

1. In Java, everything is passed by value. There is no pass by reference.
2. In Java, objects are never passed.

Variables of non-primitive types in Java are references to objects. They are not the objects themselves. If you call a method that takes an argument of a non-primitive type, then you are passing a reference (not an object) to the method. Note that that reference is passed by value. For more details, see the JavaRanch Campfire story Pass-by-Value Please.

It's a bit confusing at first, but it's important to understand how variables work in Java and get your terminology straight.
 
geo yo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got links and explanations enough to clarify my doubt.Thankyou very much for the quick response.
 
Campbell Ritchie
Marshal
Posts: 80091
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by geo yo:
I got links and explanations enough to clarify my doubt.Thankyou very much for the quick response.

You're welcome.

 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic