Professional Documents
Culture Documents
Java FAQ
Recent Topics
Register / Login
Author
Naresh Chaurasia Ranch Hand Joined: May 18, 2005 Posts: 309
Can a synchronized non-static method call synchronized static method and viceversa.
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
Michael Angstadt Ranch Hand Joined: Jun 17, 2009 Posts: 272
I like...
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
SC JP 6 || SC WC D 5 Naresh Chaurasia Ranch Hand Joined: May 18, 2005 Posts: 309
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?
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.
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
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 .
https://www.coderanch.com/t/453116/java-programmer-SCJP/certification/static-static-synchronized-methods
3/3