Results 1 to 7 of 7
Thread: Global variables
- 03-15-2011, 05:08 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
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++):
How can I create/use such variable in Java?Java Code:#include <iostream> int globalVar =0; int main() { ... } // can use globalVar int f(int x) // also can use globalVar //... and so on...
- 03-15-2011, 05:26 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-15-2011, 05:29 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Thank you very much, I didn't know that STATIC variables are "changeable" :)
- 03-15-2011, 05:36 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
-
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.
- 03-16-2011, 06:02 AM #6
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
- 03-16-2011, 09:10 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
1 of 8 global variables not visible
By Bobbo in forum New To JavaReplies: 7Last Post: 12-02-2010, 05:07 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 5Last Post: 07-08-2010, 10:50 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 1Last Post: 07-07-2010, 07:41 PM -
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


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks