Solved

A variable for two classes ????

Posted on 1998-07-08
9
268 Views
Last Modified: 2013-11-23
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.
0
Comment
Question by:Deladier
  • 5
  • 4
9 Comments
 

Accepted Solution

by:
drasin earned 60 total points
ID: 1225495
As an object-oriented language, Java has no global variables.  The proper way to have another class access a variable is through an accessor, although if the variable is public it can be directly accessed from outside the class.
So...
I assume the class Two constructor is being called somewhere in Class 1, you are probably doing something like:
public class One{
.
.
init(){
Two 2 = new Two();
Two.show();
.
.
}

Instead, you should do it like this:
public class One{
Two thisTwo;  //thisTwo is an instance of Two
.
.
init(){
thisTwo = new Two();
Two.show();
.
.
}

Then, if you want to completely violate encapsulation and do it the _wrong_ way, you can simply call
thisTwo.something
and this will give you the value of the variable something, or
thisTwo.something = "afl;kj" to set the value to what you want.

If you want to do it the _right_ way, you will observe the OO maxim which requires that an objects variables only be accessible through its methods, and you will make two methods in the class Two:
class two extends Dialog{
String something;
.
.
getSomething(){
return Something;
}
setSomething(String s){
Something = s;
}
.
.
}

And use the setSomething method when you construct the class to set the string as appropriate.  Then, when you need to access the variable from your One class, you can simply call Two.getSomething() and it will return your string.

I hope:
1) this gives you the information you need, and
2) this encourages you to do it the right way.  Preserve encapsulation.  Use good OO technique.
0
 

Expert Comment

by:drasin
ID: 1225496
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...
0
 

Author Comment

by:Deladier
ID: 1225497
Thanks for your answer, but I'm using JDK 1.0.2, could you adapt it, please?.

   Deladier.
0
 

Expert Comment

by:drasin
ID: 1225498
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
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Deladier
ID: 1225499
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.
0
 

Author Comment

by:Deladier
ID: 1225500
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.
0
 

Expert Comment

by:drasin
ID: 1225501
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
0
 

Author Comment

by:Deladier
ID: 1225502
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.
0
 

Expert Comment

by:drasin
ID: 1225503
Nice quote.  It was not ment as an insult--I am self-taught as well.  Glad I could help you
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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

19 Experts available now in Live!

Get 1:1 Help Now