Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global variabe in Java?

Status
Not open for further replies.

glwong

Programmer
Jul 13, 2001
15
US
How can I make a variable declared in one class
visible in another class?

Thanks,
Greg
 
You can declare it to be public, but this is bad programming practice. The best way to do this is to create an accessor and mutator function.
Code:
class NameHolder{

  public String getName(){
    return name;
  }

  public setName(String name){
    this.name = name;
  }


  private String name;
}
Then call getName(accessor) and setName(mutator) to access your variable.

Something along those lines is your best bet. Hope that helped. MYenigmaSELF:-9
myenigmaself@yahoo.com
"If debugging is the process of removing bugs, then programming must be the process of putting them in." --Dykstra
 
Also, if you want it to be truly global then just declare it to be static:
Code:
class NameHolder{

  public static String getName(){
    return name;
  }

  public static setName(String name){
    this.name = name;
  }


  private static String name;
}
Then whenever you want to modify this "global" name just call NameHolder.setName(String), etc... I'm pretty sure this should work. I get my languages confused sometimes. Good Luck MYenigmaSELF:-9
myenigmaself@yahoo.com
"If debugging is the process of removing bugs, then programming must be the process of putting them in." --Dykstra
 
just declare them PUBLIC STATIC

for example:

import java.awt.*;

public class YOURCLASS {

public static int NUMBER;



Lupine
RobertCorrina@patownsend.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top