• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How does call by reference happens in static method?

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


How does call by reference happens in static int [] insertAt(....) method?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly the same way as in/for non-static methods. What is your doubt?
 
Raja George Kavalam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to pass the arguments ie. an array of int values to the static method insertAt(int [] pindex) and in java all the method calling is done by call by value.

When you try to execute the pogram you can see,the changes to the pindex[] that is passed on to the that method is reflected in the calling method getTop5(), ie. somthing simmilar to call by reference happens here.




The objects ie all the classes which derive from the base Object class are passed as reference in functions. Just that the pointer handling is hidden in Java. ie say void foo(List l ){ l.add....} The changes in the list l are reflected in the calling function.
From Java Programing Language Student Guide (Sun, page 3-27): The Java Programming language only passes arguments by value. When an object instance is passed as an argument to a method, the value of argument is a reference to the object. The contents of the object can be changed in the called method, but the object reference is never changed.
]

There is nothing as such "Call by Reference", everything in Java is passed by value When you pass a primitive value as an argument to a function you pass a copy of the variable.

However when you pass a reference you pass a copy of the reference thus making to function parameter pointing to the same object, this is again call by value but what gets passed is a copy of reference, so the called function has access to the same object and thus it can manipulate the data members owned by the object.



reference :
[URL=http://faq.javaranch.com/view?CallByReferenceVsCallByValue]http://faq.javaranch.com/view?CallByReferenceVsCallByValue[/URL
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays are objects in Java - so what gets passed by value is the reference to the array object.

So if you change the reference, say by writing

pindex = new int[42];

that doesn't change the reference of the caller.

Changes to the array object itself, that is changing it's content, will be seen everywhere you have a reference to that object.

Does that help?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic