Solved

sharing a common variable in 2 classes

Posted on 2001-07-22
7
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

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…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

695 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