Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to invoke a variable of another class in Java?

 
Ziya Bakan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ziya Bakan wrote: i can only add sth.


What is sth?
 
amit punekar
Ranch Hand
Posts: 544
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ziya Bakan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic