Global declareing of varibles
hey can some one please help me.
i have three files, one apllication and two files containg methods, all three of these files are linked
how do i declare a integer that i can adjust in all three different files
and i want the changes that i make in the one file to show in the other file
please can someone help me
Re: Global declareing of varibles
sorroy for the bad english, i am using this for a game, one file works as the shop where the player can buy or sell goods, the other array handels fighting, when the player kills a enemy he gets money,
how do show the increase of money from the fighting screen in the shop screen
Re: Global declareing of varibles
There are no global variables in Java; the closest you can get is a singleton with a couple of setters and getters; something like this:
Code:
public class YourGlobal {
private static YourGlobal yourGlobal= new YourGlobal();
public YourGlobal getYourGlobal() { return yourGlobal; }
private int theInt;
private YourGlobal() { }
public void setInt(int theInt) { this.theInt= theInt; }
public int getInt() { return theInt; }
}
kind regards,
Jos
Re: Global declareing of varibles
Soryy i am quite new to this.
how would i change the value of the int from a file
and would the code that you gave me go in a seperate file or in the file which is the application
Re: Global declareing of varibles
Quote:
how would i change the value of the int from a file
You would have to read in the value from the file and use an assignment statement to change the value of the int variable.
Re: Global declareing of varibles
thanks but how would i change the ibnt from the second file with methods if a read it in in the first file and how would i make the changes show in the first file
Re: Global declareing of varibles
What are the "first" file and "second" file?
How many disk files are you writing the data to and reading from?
Are you talking about java classes?
Re: Global declareing of varibles
Sorry i have the following files, GameApp, GameArray1, ShopWorks,
the first file is shopWorks, this is where i declare the varible money and give it a set value of 1000
how do i add 100 to the varible money from the file GameArray1 and have the results shown in the file ShopWorks and GameApp
Re: Global declareing of varibles
What is in the files you are reading?
Are you talking about java classes?
Re: Global declareing of varibles
Ya sorry i should have said that
Re: Global declareing of varibles
Quote:
how do i add 100 to the varible money in the class GameArray1 and have the results shown in the classes ShopWorks and GameApp
There are several ways depending on how your program is written.
One way: When an event (changing the value of a variable) happens in one class and you want other classes to know about that event, the class where the event (variable value changed) occurs could call methods in the other classes to tell them that the event has happened.
Re: Global declareing of varibles
thanks that will work
what other ways could it be done
Re: Global declareing of varibles
Quote:
have the results shown
What do you mean by the above?
The other ways are not good ways to do it, like use public variables in the other classes.
Re: Global declareing of varibles
You said there are other ways to do it but it is fine thanks i got it to work