aspose file tools*
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
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: 12788
    
    5
What do you think "synchronize a class" means?

Bill
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: 4392
    
    8

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: 4392
    
    8

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: 38873
    
  23
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: 14153
    
  18

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: 72
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: 14153
    
  18

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