Trivial Java question

  • Thread starter Chandrashekar Tippur
  • Start date
C

Chandrashekar Tippur

All,

I have a trivial Java question.
I want to change the value of one variable on the calling object. How
do I do that in Java.

For example,
I have a Vector "abc" in Class "A" and would like to change this in
class
"B".
In class A, I instantiate B (As in B XX=new B(abc);) with abc and down
the line I would like to change the value of abc.

Please let me know if I am not too clear. I will try to reword it. The
code is quite big and I don't want to clutter the message.

Thanks in advance,
Shekar
 
R

Roedy Green

I have a trivial Java question.
I want to change the value of one variable on the calling object. How
do I do that in Java.

We just went through this a couple of days ago. You don't do that in
Java. You return a new value and the caller makes the change.
Alternatively you pass an object, and the callee sets one or more of
its fields. A callee CANNOT change the local variables of its caller.

Local mean LOCAL!!
 
H

hiwa

All,

I have a trivial Java question.
I want to change the value of one variable on the calling object. How
do I do that in Java.

For example,
I have a Vector "abc" in Class "A" and would like to change this in
class
"B".
In class A, I instantiate B (As in B XX=new B(abc);) with abc and down
the line I would like to change the value of abc.

Please let me know if I am not too clear. I will try to reword it. The
code is quite big and I don't want to clutter the message.

Thanks in advance,
Shekar

A.java
public class A{
private Vector abc;

public A(){
abc = new Vector();
...
}
...
...
public Vector getAbc(){
return abc;
}
...
...
}

B.java
public class B{
A a;

public B(){
a = new A();
...
}

void useAbc(){
Vector vec = a.getAbc();
...
...
}
...
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top