Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Global parameters passed between Classes

 
david solomon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic