Results 1 to 7 of 7
  1. #1
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default Global variables

    What if i want to use non-static ("changeable") global variable (available for all classes in file) like such one (example from C++):

    Java Code:
    #include <iostream>
    int globalVar =0;
    int main() { ... } // can use globalVar
    int f(int x) // also can use globalVar
                   //... and so on...
    How can I create/use such variable in Java?

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

    Default

    Quote Originally Posted by nikkka View Post
    What if i want to use non-static ("changeable") global variable (available for all classes in file) like such one (example from C++):

    Java Code:
    #include <iostream>
    int globalVar =0;
    int main() { ... } // can use globalVar
    int f(int x) // also can use globalVar
                   //... and so on...
    How can I create/use such variable in Java?
    There are no global variables in Java; what comes nearest is this:

    Java Code:
    public class Globals {
       public static int globalVar= 0;
    }
    ... or you could create a singleton Globals object that holds the globalVar (which is quite useless here)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Thank you very much, I didn't know that STATIC variables are "changeable" :)

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

    Default

    Quote Originally Posted by nikkka View Post
    Thank you very much, I didn't know that STATIC variables are "changeable" :)
    Yes they are; it are the 'final' variables that are not assignable. Don't build your applications with the scenario I outlined; it would be bad design.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    The thing about static/class variables is that once they are changed, new instances of objects in that class will hold the value of changed static value rather initial value.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    No, instances of classes do not "hold' a static variable. Static variables belong to the class, not to any instance.

    All instances of all classes -- any line of code that accesses a static variable of any class -- will always see the current value of that variable.

    db

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Don't build your applications with the scenario I outlined; it would be bad design.

    kind regards,

    Jos
    I would just like to reiterate this.

Similar Threads

  1. 1 of 8 global variables not visible
    By Bobbo in forum New To Java
    Replies: 7
    Last Post: 12-02-2010, 05:07 AM
  2. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 07-08-2010, 10:50 AM
  3. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 07:41 PM
  4. global variables
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-08-2009, 07:19 PM
  5. Declaring global variables
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 12:11 AM

Posting Permissions

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