Results 1 to 14 of 14
Thread: Global declareing of varibles
- 12-08-2011, 11:51 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
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
- 12-08-2011, 12:13 PM #2
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
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
- 12-08-2011, 12:59 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
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:
Java 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; } }
JosBuild a wall around Donald Trump; I'll pay for it.
- 12-08-2011, 04:09 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
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
- 12-08-2011, 04:22 PM #5
Re: Global declareing of varibles
how would i change the value of the int from a file
- 12-08-2011, 05:33 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
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
- 12-08-2011, 05:36 PM #7
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?
- 12-08-2011, 05:47 PM #8
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
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
- 12-08-2011, 05:53 PM #9
Re: Global declareing of varibles
What is in the files you are reading?
Are you talking about java classes?
- 12-08-2011, 05:58 PM #10
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Global declareing of varibles
Ya sorry i should have said that
- 12-08-2011, 06:06 PM #11
Re: Global declareing of varibles
how do i add 100 to the varible money in the class GameArray1 and have the results shown in the classes ShopWorks and GameApp
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.
- 12-08-2011, 06:12 PM #12
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: Global declareing of varibles
thanks that will work
what other ways could it be done
- 12-08-2011, 06:14 PM #13
Re: Global declareing of varibles
have the results shown
The other ways are not good ways to do it, like use public variables in the other classes.
- 12-09-2011, 11:10 AM #14
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
java:global
By flanjl2 in forum New To JavaReplies: 3Last Post: 09-23-2011, 10:35 AM -
Set Global variable value
By SteveBMill in forum AndroidReplies: 2Last Post: 09-19-2011, 12:48 AM -
Global variables
By nikkka in forum New To JavaReplies: 6Last Post: 03-16-2011, 10:10 AM -
global variables
By blackstormattack in forum New To JavaReplies: 1Last Post: 03-08-2009, 08:19 PM -
Global constants
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 10:06 AM
Bookmarks