?
Solved

sharing a common variable in 2 classes

Posted on 2001-07-22
7
Medium Priority
?
226 Views
Last Modified: 2011-10-03
I have 2 classes runnung 2 different ActionListener

How could I return a variable to the 1st class  after executing it in the second class.

Effectively, a need to call a second class because I need it to run an algorithm, but it uses AcionListener and I can't call a

return thisvariable to the first.

Is there a way to share the common variables between the 2 classes??

Hope you know what I mean?
0
Comment
Question by:TLTEO
7 Comments
 

Expert Comment

by:sengsational
ID: 6306084
What I like to do is create a 'local memory' object.  This is a very simple class with all of the things I want in common (instance variables).  I instantiate one of these, and 'everybody' can read and write to it.  You just need to pass the reference to this object around to all of your classes.

--Dale--
0
 
LVL 10

Expert Comment

by:Nushi
ID: 6306268
i agree with sengsational .

create a class like this:

public class vars{

public static int var1=...
public static int var2...
}

and from another class cal the vars of this class and you will get what u need.

Nushi.
0
 

Author Comment

by:TLTEO
ID: 6306299
I have 2 class,  let's call class first and class second.

class first{
..
..
public void actionPerformed(ActionEvent e)
.
.
if(clickbutton==thisButton)
{
second sec= new second();
.
.
}


class second{
..
..
public void actionPerformed(ActionEvent e)
.
.
if(clickbutton==thisButton)
{
String thisvariable= getalgorithm(...)
.
.
}


How can I pass the string thisvariable  back to the first.class ??

I can use return method becos it is under void actionPerformed(...)





0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
superschlonz earned 80 total points
ID: 6306317
You could give a reference to first in the constructor of second:

class first{
..
..
public void actionPerformed(ActionEvent e)
.
.
if(clickbutton==thisButton)
{
second sec= new second();
.
.
}
.
.
public void putThisVariable( String thisvariable )
{
.
.
}  
}

class second{
private first caller;
public second( first aCaller )
{
  caller = aCaller;
}
..
..
public void actionPerformed(ActionEvent e)
.
.
if(clickbutton==thisButton)
{
String thisvariable= getalgorithm(...)
caller.putThisVariable( thisvariable );
}

You could also create an interface with the method putThisVariable and implement it in first and reference it in second so first could be exchanged by another class which implements the interface.
0
 

Author Comment

by:TLTEO
ID: 6306333
IC.   seems to be getting it now. I am happy that you understand what I meant. Tot I have read this method before but forgot. Let me test it and return to you again . Cheers superschlonz
0
 

Expert Comment

by:sengsational
ID: 6320595
Here's another way.  You pass a reference of the first class to the second.  That way you can run the setMyString method.  Or you could just make myString public and set it directly.

class first{
private String myString;
..
public void setMyString(String s){
myString = s;
}
..
public void actionPerformed(ActionEvent e)
.
.
if(clickbutton==thisButton)
{
second sec= new second(this);
.
.
}


class second{
private first otherclass;
// constructor
public second(first t){
otherClass = t;
}
..
..
public void actionPerformed(ActionEvent e)
.
.
if(clickbutton==thisButton)
{
String thisvariable= getalgorithm(...)
otherClass.setMyString(thisvariable);
.
.
}
0
 

Author Comment

by:TLTEO
ID: 6345560
tks to sensational too. great help cheers
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question