Results 1 to 14 of 14
  1. #1
    Programmm is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default 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

  2. #2
    Programmm is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default 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

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default 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; }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Programmm is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default 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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Global declareing of varibles

    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.

  6. #6
    Programmm is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default 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

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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?

  8. #8
    Programmm is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default 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

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Global declareing of varibles

    What is in the files you are reading?

    Are you talking about java classes?

  10. #10
    Programmm is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Global declareing of varibles

    Ya sorry i should have said that

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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
    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.

  12. #12
    Programmm is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Global declareing of varibles

    thanks that will work

    what other ways could it be done

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Global declareing of varibles

    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.

  14. #14
    Programmm is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: Global declareing of varibles

    You said there are other ways to do it but it is fine thanks i got it to work

Similar Threads

  1. java:global
    By flanjl2 in forum New To Java
    Replies: 3
    Last Post: 09-23-2011, 09:35 AM
  2. Set Global variable value
    By SteveBMill in forum Android
    Replies: 2
    Last Post: 09-18-2011, 11:48 PM
  3. Global variables
    By nikkka in forum New To Java
    Replies: 6
    Last Post: 03-16-2011, 09:10 AM
  4. global variables
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-08-2009, 07:19 PM
  5. Global constants
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 09:06 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •