Link to home
Start Free TrialLog in
Avatar of Deladier
Deladier

asked on

A variable for two classes ????

Hi, in an applet with two classes, How do I make that a variable be known in both classes?.
    Per example, in an applet with classes named "One" and "Two", class "Two" has a variable named "something", how do I pass this String from class "Two" to class "One"?.

    import java.awt.*;

    public class One extends java.applet.Applet {
            .
            .
            .
     }

      class Two extends Dialog {
            String something = "abcde";
                    .
                    .
                    .
      }


Thank U.
ASKER CERTIFIED SOLUTION
Avatar of drasin
drasin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drasin
drasin

I'm sorry-it should be

thisTwo.show(), //the instance, not the class name.

I'm sure you already knew that, but just in case...
Avatar of Deladier

ASKER

Thanks for your answer, but I'm using JDK 1.0.2, could you adapt it, please?.

   Deladier.
This is not a jdk specific issue.  If the code is not working, it is probably because of a typo on my part.  Let me clean it up for you and maybe this will make things clearer:


public class One extends java.applet.Applet{
thisTwo = new Two;
/**
 thisTwo is an instance of the class Two.  Visible throughout One
*/
.
 .
init(){
/**this is the method thrown when the applet is intialized.  You may be using start()
 instead, it makes no difference in this case
*/
thisTwo = new Two();
/**
set the value of the field thisTwo to a new instance of the class Two
*/
thisTwo.setSomething("Hello World);
thisTwo.show();
.
.
} //end class One

class two extends Dialog{
String Something; //we will use the get and set to change this variable
 
 
getSomething(){
return Something;
}

setSomething(String s){
Something = s;
}
 
 
}

Thus, in class two, the field: Something; and in Class one, the methods: thisTwo.getSomething/thisTwo.setSomething all are dealing with the same variable.

Hope this helps
Thanks, it's already working fine. That's great!.  :)

I'd like to ask you another question related for 10 points more:
In class One, how do I know when Dialog window is closed?.

  Thanks in advance.
Thanks, it's already working fine. That's great!.  :)

I'd like to ask you another question related for 10 points more:
In class One, how do I know when Dialog window is closed?.

  Thanks in advance.
I'm not quite clear on what you mean.  If you mean, at any given time can class One make a query to find out wether or not class Two is open, then I believe that the method thisTwo.isShowing() whould return true if the window is open, false if not.  If you mean to trap the event of the window closing, then you need to do two things:
 (this is the 1.02 method, you would handle it slightly differently in 1.1)

1) in your class One, make a method which you want thrown at the time the window closes:
Class One{
Two thisTwo;
.
.
init(){
thisTwo = new Two();
thisTwo.setThisOne(this);  //more on this line in a moment
thisTwo.show();
.
.
}

myMethod{
/**
put here the code you want to execute in class One when the window
closes
*/
}
.
.
}

Then, in class Two, override the handleEvent(Event e) method and call this method in Class One.  Note that class Two will need a reference to the applet in order to use its methods, so  class Two will look something like this

class two extends Dialog{
String Something;
One thisOne;//again, we will ues a get and set method to chang this variable
.
.        
getSomething(){
return Something;
}

setSomething(String s){
Something = s;
}
.
.
getThisOne(){
return thisOne;
}

setThisOne(One o){
thisOne = o;
}

handleEvent(Event e){
if (e.id == Event.WINDOW_DESTROY){//this might be evt.key, don't remember-try both
thisOne.myMethod()
}
I don't mean to sound condescending, but I think that maybe some of the difficulty you are having comes from not having a complete grasp of object-oriented programming as it relates to Java.  You might want to consider using some sort of visual modeling (UML, for example) to aid in visualizing your development.  A number of resources for this are availible on the internet.  The encapsulation of methods and data from one object to others should be a tool to exploit in development, not an impediment to development.  Sermon over.
hope this helps
Actually I am a self-taught person and for me a problem means a challenge not a weakness.

I like this frase:
"El que cae jamás se humilla sino que aprende, el que aprende se supera, y el que se supera no vuelve a caer jamás".
         Jigoro Kano.

Anyway, thanks for all your help.

   Deladier.
Nice quote.  It was not ment as an insult--I am self-taught as well.  Glad I could help you