GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Global  parameters passed between Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Global  parameters passed between Classes" Watch "Global  parameters passed between Classes" New topic
Author

Global parameters passed between Classes

david solomon
Greenhorn

Joined: Nov 08, 2003
Posts: 7
Hi ,
I am trying to define global(commonly shared variables/strings between different classes) variables to be shared and can be manipulated between many classes, here is the example:
// this class suppose to hold the definition and initialisation of the global variables
import java.awt.*;
public class GlobalVars
{
public String gstrx ; // this is my global variable to be modified in the next class
public void init()
{
gstrx = "Hi";
System.out.print(" gstrx from class GlobalVars =" + gstrx );
}
}
The above class was saved in file GlobalVars.java and loaded to the project.
import java.awt.*;
import java.applet.Applet;
public class class1 extends Applet
{
GlobalVars MyGclass = new GlobalVars();
String strx1;
String strx2;
public void init()
{
MyGclass.init();
strx1 = MyGclass.gstrx + " from me ";
System.out.print("strx1 from class1 =" + strx1);
//here is where I need to modify gstrx in this class and in GlobalVars class
MyGclass.gstrx = "Welcome from me "; //this will modify it for this class only!
System.out.print("MyGclass.gstrx = " + MyGclass.gstrx );
strx2 = MyGclass.gstrx;
System.out.print("strx2 from class1 = " + strx2);
}
}
The above class was saved in file class1.java and loaded to the project.
What really I wanted was that �strx2� would show �Welcome from me�, which means I have modified the global variable in my global class �MyGclass�. Any help on this?.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

David,
You can make it a static variable. That means that there will only be one copy, regardless of how many instances of the class you have. Also, you should consider making the variable private and using a getter/setter.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Give some thought to threading issues as well.

That test could return false if some other thread changes the variable to "Bob" in between your two lines of code.
Is this something that will update often? I use statics like that for things that change rarely, such as a debug or log switch setting that normally never changes, but can be set on the fly by an administrator if necessary.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Hi David, welcome to JavaRanch. Unfortunately this forum here is for Servlets, and you question is about parameters and variables. I am going to move this thread to the more appropriate Java Beginner's forum.
Also, avoid Global Variables. And I mean in any code, in any language, whenever and whereever you can. They can get you into a lot of trouble. In Maintenance and adding enahncements.
As was said. Static Variables work like Global. And also think about just having a class that has instance variables, and pass around a reference to the class itself to all these other classes.
Thanks
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
Consider Paul's rocket mass heater.
 
subject: Global parameters passed between Classes