• 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

pass an object

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we pass an object to a method?If answer is yes. In which situation it is important(necessary) to pass an object to a method.Give any exmple.
Thanking in anticipation
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by atif toor:
can we pass an object to a method?If answer is yes. In which situation it is important(necessary) to pass an object to a method.Give any exmple.
Thanking in anticipation


The quick answer is 'Yes, you can'.
As to why you would, objects allow 'pass by reference' as opposed to 'pass by value'.
If you pass a primitive into a method (int, boolean etc) and the method changes the value then the variable remains UNCHANGED upon exiting the method.
If you pass an object into a method, if the method alters the object value, it's value remains changed once the method is completed.
There is an important addendum and that is immutable types. An example: a String is an immutable type, so whenever you think you are altering it's value, what you are really doing is creating a NEW object.
The importance of this is that if you pass a String to a method, then that method changes the String, the method retains its reference to the new object but once the method exits the reference is to the ORIGINAL String since that reference wasn't changed, only the reference to the String object within the method.
Phew. Hope you got all that.
Dave.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by atif toor:
can we pass an object to a method?If answer is yes. In which situation it is important(necessary) to pass an object to a method.Give any exmple.
Thanking in anticipation


Actually, while I have the soap box, as to when it is necessary to do so:
There are many and varied reasons related to OOAD, but a method which accepts as an argument an object of type 'A' will also accept any classes that extend class A.
Dave.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes we can!
when? whenever u feel that the object should be passed by reference. now what do u mean by that .. whenever u want to pass a object to a function and the function is changing the contents of the obj. and that changed value of the obj. u want in the calling function than u will be using pass by reference.
hope u get the point
Rajesh Purohit
INDIA

Originally posted by atif toor:
can we pass an object to a method?If answer is yes. In which situation it is important(necessary) to pass an object to a method.Give any exmple.
Thanking in anticipation


 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost everything in java is an object. Therefore almost everything that is passed will be an object of some sort.
String s = "Cindy";
System.out.println(s); //pass the string object s to println
or to be more whimsical
Person p = new Person();
Employee emp = new Employee(p);
[This message has been edited by Cindy Glass (edited May 03, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic