This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how can I synchronize a class in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can I synchronize a class in java" Watch "how can I synchronize a class in java" New topic
Author

how can I synchronize a class in java

Bandita Patel
Greenhorn

Joined: Mar 23, 2010
Posts: 25
Hi Team,
Kindly let me know how can we Synchronize a class in Java.

Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
What do you think "synchronize a class" means?

Bill


Java Resources at www.wbrogden.com
Abhay Choubey
Ranch Hand

Joined: Jun 02, 2011
Posts: 34
I did not get you. can you be more specific. please

Abhay
Bandita Patel
Greenhorn

Joined: Mar 23, 2010
Posts: 25
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Mar 23, 2010
Posts: 25
Thanks Matthew...
ya exactly i think this should be the answer..

But is it possible by declaring a class as 'static'..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Oct 13, 2005
Posts: 36478
    
  16
There should be some useful information in the Java Tutorials.
Abhay Choubey
Ranch Hand

Joined: Jun 02, 2011
Posts: 34
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

Joined: Aug 16, 2005
Posts: 13869
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 70
No you can't declare a class as static only methods and variables.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

Shamsudeen Akanbi wrote:No you can't declare a class as static only methods and variables.

Are you sure? Try to compile this:
 
 
subject: how can I synchronize a class in java
 
Similar Threads
Static variable Question?
Sychronized class
Some good questions here. Please check
Synchronize JSP
How to Synchronize a class??