jQuery in Action, 3rd edition
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

Global parameters passed between Classes

david solomon

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()
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

Joined: May 26, 2003
Posts: 33124

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, 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

Joined: Feb 05, 2001
Posts: 17276

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.

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link: http://aspose.com/file-tools
subject: Global parameters passed between Classes
It's not a secret anymore!