Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

A variable for two classes ????

Posted on 1998-07-08
9
Medium Priority
?
298 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
[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
  • 5
  • 4
9 Comments
 

Accepted Solution

by:
drasin earned 240 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

722 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