Solved

sharing a common variable in 2 classes

Posted on 2001-07-22
7
220 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
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!

 
LVL 3

Accepted Solution

by:
superschlonz earned 20 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
How to configure empty element in XML Document parser? 15 44
Notify sent to other threads in Java 9 41
web project error add remove 1 49
dao vs facade design patterns 2 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

685 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