File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67
Hi,

Every one,


1. Is that synchronized is applicable to variables "static variables" if yes how. if not why?













Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

What is your exact question? Do you mean whether a static variable can have synchronized access or not?


Swastik
veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67
Yes, thats the question.

As we make methods synchronized by prefixing it with synchronized keyword, same thing can be done with static variable?

Possible? if yes how, and no Why?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Lets keep aside the word static. Can you synchronize a variable? I guess no, you can't a prefix a variable with synchronized keyword. It may be applied to a method or a block of code, and of course you can acquire a lock on any object.

veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67

Thanks for the reply, but what is the reason? why it cant be?

Any idea?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

veena bijur wrote:
Thanks for the reply, but what is the reason? why it cant be?

Any idea?

1) Why is usually not a constructive question: it comes down to a decision made by the language designers. You aren't likely to find them here, and even if you did it would be unlikely they would remember. So the answer to why is usually 'because that is what the language rules say'.

2) A better question is how do you think it should behave? Maybe (hint hint) there is a different keyword that will do what you expect a 'synchronized variable' to do.


Steve
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

veena bijur wrote:
Thanks for the reply, but what is the reason? why it cant be?

Any idea?


What do you think it means to "make a method synchronized"? Do you know exactly what happens when you do that?

Once you understand that, what would you expect it to mean if we could "make a variable synchronized"?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads