File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Variable value passing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable value passing " Watch "Variable value passing " New topic

Variable value passing

Ranch Hand

Joined: Feb 26, 2005
Posts: 35
I am having issue with passing variable value from one class to another.
I have class A and Class B bot in same package.

package D
Class A {

private int j = 0; //I have getters and setters for this variable

public void doSomething(){


Class B{
A a = new A();
int i = a.getJ();

System.out.println("value of j is "+i);


My problem is class A gets called first and j's value get incremented becomes 2. But when i am calling the getter value in i received is 0 (initial value of J).
Someone recomened since A does not know about method getJ in B i use request object to set value of j in B which i did but result is still same i am getting value as 0. I tried using request dispatchr but than also am not getting right value of J in A.

I know this is basic issue in java if anyone canhelp me i will be really grateful.
Pablo M�ller
Ranch Hand

Joined: Dec 12, 2006
Posts: 63

j++ and ++j both increment j even if they are not assigned. That's why you get j incremented by 2.

Regarding the other error you have.. is that your actual code.. I mean, is your class B exactly what you posted?

2B || !2B
I agree. Here's the link:
subject: Variable value passing
It's not a secret anymore!