File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable value passing

 
zaxxon25
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(){
j++;
this.setJ(++j);
}

}

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 twice...it 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
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi



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?
Pablo.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic