Results 1 to 4 of 4
- 11-01-2011, 12:39 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 53
- Rep Power
- 0
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:
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.Java Code:private int[][] globalarray = new int[8][9]; public classins[] ClassInstances = new Classins[6]; public int lastused,State;
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:
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.Java Code:public int SetExample(int set) { int example = set; //must be initialized, making it 0 everytime its called return example; }
Any advice on how to reduce the number of globals would be good :)
- 11-01-2011, 01:31 AM #2
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.
- 11-01-2011, 11:56 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 53
- Rep Power
- 0
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?
- 11-01-2011, 10:27 PM #4
Re: Reducing number of global variables
That will change their accessibility not their scope.
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:class Foo { private int var1; public int var2; public void someMethod() { int var3; } }
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.Java Code:public void anotherMethod() { { int var4 = 100; } System.out.println(var4); // error }
Similar Threads
-
having trouble with global variables
By cloutier172 in forum New To JavaReplies: 3Last Post: 10-31-2011, 06:22 PM -
Global variables
By nikkka in forum New To JavaReplies: 6Last Post: 03-16-2011, 09:10 AM -
1 of 8 global variables not visible
By Bobbo in forum New To JavaReplies: 7Last Post: 12-02-2010, 05:07 AM -
global variables
By blackstormattack in forum New To JavaReplies: 1Last Post: 03-08-2009, 07:19 PM -
Declaring global variables
By eva in forum New To JavaReplies: 3Last Post: 12-23-2007, 12:11 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks