File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
veena bijur
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Every one,


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













 
Swastik Dey
Rancher
Posts: 1601
5
Android Java Java ME
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your exact question? Do you mean whether a static variable can have synchronized access or not?
 
veena bijur
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1601
5
Android Java Java ME
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Any idea?
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic