• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Call by reference related >>>>

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class callby
{
int a=2;
}
class callby1
{
void change()
{
callby o=new callby();
o.a=o.a*2; //line a
System.out.println(o.a); // line b
}
public static void main(String[] arg)
{

new callby1().change();
System.out.println(new callby().a); // line c
}
}


/* in above code sample i am trying to change the value of 'a' initialized in 'callby' by calling it's reference.at 'line a' i am altering value and storing it to the 'a' of callby.
So it should be chnged forever.but it's not happening.
At line c it's printing 2 ???

Can you help me out.
*/
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

You are creating two different objects at line a and line c

You are changing value of a it for object created in line a and not for object created in line c.
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i was trying this example to understand the meaning of call by reference.as i know about this is that we can change the value of particular variable of any class by calling it's object reference.Same i was doing.
And if the new value is assigned at line a so at line c it should be new one.
**************************************
If i wrong at call by reference concept,please suggest me.

Thanks
Deepak
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By 'line a' does it not mean that new value is being assigned to original copy of class callby ???
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point here is that you are making two objects by writing the keyword 'new' twice. You will not get the reference to the object created in the change() method by any means into your code unless you declare the variable 'o' in line a as global or return its reference.

The line c is making a fresh object of 'callby' and is not having any sort of connection with the object 'o' modified in line a.

A valid exmaple for call by reference could be :

class callby
{
int a=2;
}
class callby1
{
void change(callby o)
{
o.a=o.a*2; //line a
System.out.println(o.a); // line b
}
public static void main(String[] arg)
{
callby c=new callby();
new callby1().change(c);
System.out.println(c.a); // line c
}
}

Hope this solved your problem.

Regards.
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So am i wrong to think ,by callby reference we can permanently change object's value.
as if i replace line c with

System.out.println(new callby().a); // line c

it still prints 2.but according to me now it should be 4.
And to avoid this we use Bean setter and getter methods.

I don't know where am i wrong??
**********************************************

Bit confused???/

Please suggest me.
Thanks
Deepak
 
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
So am i wrong to think ,by callby reference we can permanently change object's value.

No, you are not wrong about how 'pass by reference' works.

it still prints 2.but according to me now it should be 4.

Subha already tried to explain this to you: The variable a in class callby is an instance variable. Each object that you create out of class callby will have its own variable named a, which will be initialized to the value 2. This is what happens in your original example:

1. In the line before "line a", you create a new callby object. The value of its variable a is initialized to 2.
2. In "line a", you multiply the value of a in that object by 2, so the value is now 4.
3. In "line b", you print the contents of the variable a in the object, so you get 4.
4. In "line c", you create a new, different callby object. It has its own variable a which is initialized to 2, and that's what you print.

Your example doesn't do anything with 'pass by reference'.

Have a look at these topics in Sun's Java Tutorials:

Object-Oriented Programming Concepts
Classes and Objects
Understanding Instance and Class Members

By the way, 'pass by reference' does not exist in Java; Java is strictly 'pass by value'. To understand this, read the JavaRanch Campfire story Pass-by-Value Please.
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jasper

But my question is why should we use bean's setXxx or getXxx to let access class variables.As they are going to be changed forever.as in my example int a=2; is changed to 4 only for 1 instance of clallby nto permanently for all instances .it is not affecting rest of the instances.

Then why should i access instance variables via setXxx or getX... making variables private.

Why is it emphasized to access variables via setter or getter .

Please help me out???

Thanks
Deepak
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic