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 accessing variables of other classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing variables of other classes" Watch "accessing variables of other classes" New topic

accessing variables of other classes

madhup narain
Ranch Hand

Joined: Dec 14, 2004
Posts: 148

can someone tell me how to access a variable of another class. without inheritence without modifying the return parameters of method getData().
i want to get the value 10 stored in str_int

Money for nothing and Java for Free
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

Unless there's more code, it doesn't look the value of like str_int gets saved, the input gets assigned to Integer(10) and then thrown away since you can't reassign an input object's reference value.

[OCA 8 Book] [Blog]
Sachin Dimble
Ranch Hand

Joined: Dec 07, 2005
Posts: 100
Mahup, u can make str_int as instance variable of class Data after calling getData() can access the changed value of str_int!

With Best Regards,
Sachin Dimble
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
Better way is to add a getter function to the data class that returns the variable value you want (e.g., getStrInt()).
I agree. Here's the link:
subject: accessing variables of other classes
It's not a secret anymore!