Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can I synchronize a class in java

 
Bandita Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Team,
Kindly let me know how can we Synchronize a class in Java.

Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think "synchronize a class" means?

Bill
 
Abhay Choubey
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not get you. can you be more specific. please

Abhay
 
Bandita Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example we can Synchronize a method or block of statements by using the keyword 'Synchronized'..

Similarly is there a way to Synchronize a class also?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't really make sense, because a class isn't a block of executable code. But you can mark all the methods as synchronized, in which case only one can be called at a time. Is that what you mean?
 
Bandita Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew...
ya exactly i think this should be the answer..

But is it possible by declaring a class as 'static'..
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You'll have to synchronize each method.

"static" has nothing to do with synchronization, by the way, but maybe that was unintentional.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49457
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be some useful information in the Java Tutorials.
 
Abhay Choubey
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can not use synchronized key word for a class. we can use it for a method -

EX-
public synchronized void display() {
System.out.println("Hello!!!");
}
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bandita Acharya wrote:But is it possible by declaring a class as 'static'..

Nested classes can be declared static.
Top-level classes cannot be declared static.
 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you can't declare a class as static only methods and variables.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shamsudeen Akanbi wrote:No you can't declare a class as static only methods and variables.

Are you sure? Try to compile this:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic