Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Junky

Thread: Reducing number of global variables

  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Cool Reducing number of global variables

    Hello,
    i googled a number of ways to try and reduce the number of global variables i am using in my code as im told its inefficient and pollutes the global variable space. However most of the results i have seen online seem to not provide a way of storing the information on a more permanent basis, i have something like this global variable list:
    Java Code:
    private int[][] globalarray = new int[8][9];
    public classins[] ClassInstances = new Classins[6];
    public int lastused,State;
    And i would like to find a way to reduce this list down so the only global variable i need State, as it stores a variable checked by another class and if it is set to something then another variable changes.
    I have tried Get methods, however when using a get/set methods it still requires that the temporary variables within are initialized meaning i must set it to something that may be wrong eg:
    Java Code:
    public int SetExample(int set)
    {
     int example = set; //must be initialized, making it 0 everytime its called
     return example;
    }
    So get/set methods are out as they do not allow me to store a variable and change it without going back to square one.
    Any advice on how to reduce the number of globals would be good :)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Reducing number of global variables

    Java does not have global variables. It has local variables, instance variable and static variables. Get that straight.

    You should aim to declare variables in the smallest possible scope. That means if you use a variable in only one method then declare it in that method and not as an instance variable. If you only use a variable inside a loop inside that method then declare it inside the loop and not at the top of the method.
    sunde887 likes this.

  3. #3
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default Re: Reducing number of global variables

    Ok, thanks for telling me about there not being global variables. It never really occurred to me that because it is in a class that not truly global *bit of a blonde moment* but my teacher said to me that i have an uneccesary use of globals within the class i am writing. If i change their scope to be only private as you suggest do you think that will be what my teacher is getting at?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Reducing number of global variables

    Quote Originally Posted by trishtren View Post
    If i change their scope to be only private
    That will change their accessibility not their scope.
    Java Code:
    class Foo {
        private int var1;
        public int var2;
    
        public void someMethod() {
            int var3;
        }
    }
    Both var1 and var2 have the same scope: they are instance variables and accessible through out the class. Whereas var3 is a local variable and is only accessible in that method. This is what we mean by scope. Basically variables are only accessible withing the enclosing braces { } that they are declared in. Another example:
    Java Code:
    public void anotherMethod() {
        {
            int var4 = 100;
        }
        System.out.println(var4); // error
    }
    The above code will not compile as var4 is declared inside a pair of braces (which is totally fine) and cannot be accessed outside where the print statement is.

Similar Threads

  1. having trouble with global variables
    By cloutier172 in forum New To Java
    Replies: 3
    Last Post: 10-31-2011, 06:22 PM
  2. Global variables
    By nikkka in forum New To Java
    Replies: 6
    Last Post: 03-16-2011, 09:10 AM
  3. 1 of 8 global variables not visible
    By Bobbo in forum New To Java
    Replies: 7
    Last Post: 12-02-2010, 05:07 AM
  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
  •