Solved

sharing a common variable in 2 classes

Posted on 2001-07-22
7
215 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now