Results 1 to 5 of 5
- 09-02-2010, 01:29 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
- 09-02-2010, 01:51 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
You need to write static getter and setter methods and synchronize those
Java Code:private static int var; public static synchronized void setVar(int i) { var = i; } public static synchronized int getVar() { return var; }
The exact same way you "synchronize" anything else, of course.
- 09-02-2010, 02:10 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
that means if we want to use a static variable in multithreading concept we can not assign static variable like ClassName.staticvar = something from any where.
- 09-02-2010, 02:12 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Well what did I say?
Not doing that may still get the same value to all threads eventually but there is no guarantee as to when, or if as all threads will have "local" copies of the reference values.
You must, of course, then do
Java Code:ClassName.setVar(value); // and value = ClassName.getVar();
Java Code:ClassName.var = value; // and value = ClassName.var;
Last edited by masijade; 09-02-2010 at 02:15 PM.
- 09-02-2010, 02:25 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
Similar Threads
-
Still having trouble with static & non-static variables
By Psyclone in forum AWT / SwingReplies: 6Last Post: 02-15-2010, 05:31 AM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 06:48 PM -
static are instance variables
By gabri in forum Advanced JavaReplies: 12Last Post: 09-30-2008, 07:30 PM -
Using Static Variables
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 12:08 AM -
Help with static variables
By bbq in forum Advanced JavaReplies: 1Last Post: 06-28-2007, 06:38 PM
Bookmarks