Sunteți pe pagina 1din 3

7/14/13

static and non-static synchronized methods (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

static and non-static synchronized methods


posted 7/8/2009 9:53:35 PM

Naresh Chaurasia Ranch Hand Joined: May 18, 2005 Posts: 309

Can a synchronized non-static method call synchronized static method and viceversa.

SC JP 1.4, SC WC D1.4, OC A(1Z0-007)

Mo Jay Ranch Hand Joined: Feb 16, 2009 Posts: 83

posted 7/8/2009 10:18:36 PM

This has nothing to do with wether the methods are synchronized or not, this has something to do with static method calling non-static method and vice versa. Answer is NO, static methods cannot call non-static methods directly. You can instantiate the class and use the object reference to make the call. By the way there a tricky way that you can get away with the syntax but it is not considered good coding style. Cheers!!!

Eduardo Bueno
https://www.coderanch.com/t/453116/java-programmer-SCJP/certification/static-static-synchronized-methods 1/3

7/14/13

static and non-static synchronized methods (SCJP forum at JavaRanch)

Ranch Hand Joined: Jun 04, 2009 Posts: 154

posted 7/8/2009 10:22:52 PM

Non-static methods are allowed to call static methods.

Mo Jay Ranch Hand Joined: Feb 16, 2009 Posts: 83

posted 7/8/2009 10:35:23 PM

You are totally right Eduardo. Cheers!!!

Michael Angstadt Ranch Hand Joined: Jun 17, 2009 Posts: 272
I like...

posted 7/9/2009 12:11:51 AM

Like others have said, a static method cannot call a non-static method, whether it's synchronized or not. That's simple. But it is possible for a non-static synchronized method to call a static synchronized method. However, the thread that's calling the static method may need to wait for the static method's lock to be released. Static synchronized methods use the class's Class object as a lock. So the follow two methods are equivalent:
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .

c l a s sA { s t a t i cs y n c h r o n i z e dv o i df o o ( ) { / / . . . } s t a t i cv o i df o o 2 { s y n c h r o n i z e d ( A . c l a s s ) { / / . . . } } }

Non-static synchronized methods use the current object instance as a lock. So these two methods are equivalent:
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .

c l a s sA { s y n c h r o n i z e dv o i df o o ( ) { / / . . . } v o i df o o 2 { s y n c h r o n i z e d ( t h i s ) { / / . . . } } }

https://www.coderanch.com/t/453116/java-programmer-SCJP/certification/static-static-synchronized-methods

2/3

7/14/13

static and non-static synchronized methods (SCJP forum at JavaRanch)

SC JP 6 || SC WC D 5 Naresh Chaurasia Ranch Hand Joined: May 18, 2005 Posts: 309

posted 7/9/2009 11:05:56 AM

Actually i meant to ask something different . Let me rephrase my question. Suppose a thread1 in non-static synchronized block,after acquiring object lock, calls a static synhronized method, then will thread1 acquire class lock or not, since a instance method can always call static method?

Vijitha Kumara Bartender Joined: Mar 24, 2008 Posts: 3674


I like...

posted 7/9/2009 11:34:32 AM


Naresh Chaurasia wrote:

Suppose a thread1 in non-static synchronized block,after acquiring object lock, calls a static synhronized method, then will thread1 acquire class lock or not

Only if another thread hasn't aquired the class lock at that time.

SC JP 5 | SC WC D 5 [How to ask questions] [Twitter]

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: static and non-static synchronized methods

Similar Threads Thrread New question on K&B Chapter 9 Question 2 static synchronized method ... Static and Non static Synchronized methods Thread behavior with synchronized method
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 09:09:21 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

https://www.coderanch.com/t/453116/java-programmer-SCJP/certification/static-static-synchronized-methods

3/3

S-ar putea să vă placă și