• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Global parameters passed between Classes

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Evildoers! Eat my justice! And this tiny ad's justice too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic