aspose file tools*
The moose likes Java in General and the fly likes  How to invoke a variable of another class in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " How to invoke a variable of another class in Java?" Watch " How to invoke a variable of another class in Java?" New topic
Author

How to invoke a variable of another class in Java?

Ziya Bakan
Greenhorn

Joined: Feb 07, 2013
Posts: 13
Hi all!!



I have a problem at calling a variable of another class (in JAVA). Lets say we hava class1 and class2 and class2 has a method like :

public class2{

public void method2 (Element ele, Grammar utterance, String user) {

String speech =................

}

}

And my purpose is getting this "speech" variable and passing it to class1. Any ideas about how to do this, i read some forums regarding this question but didnt work for me!! Thanks a lot in advance!!!
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 123

Which one do you want?

1. Get the value of 'speech' by calling 'method2(...)'
2. Get the value of 'speech' without calling any method
Ziya Bakan
Greenhorn

Joined: Feb 07, 2013
Posts: 13
The problem is i m not allowed to change anything(for exp. from void to public or making it static) at the current code i can only add sth.Because when i change sth then so many codes get effected of this and i need a solution which does not modify the code but only adds some lines of codes!!! And this String speech is defined inside a method of class2 so it is a local variable and i dont know how to reach it.

Both are ok for me, i just need to get the value of speech from the other class but considering what i wrote above.no changesd of the current code, only additions are possible!!!
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 123

Ziya Bakan wrote: i can only add sth.


What is sth?
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 516
Hello,
If I have understood your problem correctly maybe following will help you -
1) Declare an instance variable in class 2
2) in method2() initialize this instance variable using the value of "speech"
3) Add a getter method for newly added instance variable.

Please note that this change will have threading consequences as your instance variable will not be thread safe.

Hope this helps.

regards,
Amit
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You most probably don’t want to pass anything to another class, but to an instance of the other class. The two are very different.

There are all sorts of different ways you can do that. One way is to have a field of your object which holds a reference to an instance of the other class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Ziya Bakan wrote: . . . i m . . . exp. . . . sth . . . sth . . .
Please read this. And a block of unpunctuated text is difficult to read.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

If this is really your method:


Then speech is a local variable to the method. Once the method exits, speech goes out of scope. Unless you have some other reference to the String object speech was pointing to, it become eligible for garbage collection.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ziya Bakan
Greenhorn

Joined: Feb 07, 2013
Posts: 13
Thank you so much Amit , it works correctly now



amit punekar wrote:Hello,
If I have understood your problem correctly maybe following will help you -
1) Declare an instance variable in class 2
2) in method2() initialize this instance variable using the value of "speech"
3) Add a getter method for newly added instance variable.

Please note that this change will have threading consequences as your instance variable will not be thread safe.

Hope this helps.

regards,
Amit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to invoke a variable of another class in Java?