• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Variable value passing

Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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(){


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.
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
    Bookmark Topic Watch Topic
  • New Topic