Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I am thinking of a question.
I have built a JAVA class as a servle, inside which I have a method.
I am wondering what will be different if I set this method as a static one.
I think the result should be the same, but I want to know how tomcat or JRE works in this case, in both the call of servlets and web services.
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jun-15 4:13am    
Do you understand how methods work in general and why some are static and some not (then they are instance methods). The instance methods use "this"; if not, they have to be static. Do the math.
—SA
Member 11745740 8-Jun-15 4:47am    
Yes, of course I know it. Servlet is multi-thread, I am wondering if a method has a static attribute matters.
Sergey Alexandrovich Kryukov 8-Jun-15 11:29am    
I cannot be so sure. If you knew that, you would hardly mention "static attribute" (what would that supposed to mean?) and would probably understand the implication if threading.
—SA

1 solution

Member 11745740 wrote:

Yes, of course I know it. Servlet is multi-thread, I am wondering if a method has a static attribute matters.
Well, about your "I know it" please see my comment to the question. And as to the threading, let's see:

Simple analysis could show you that using a static method add problems with multithreading, can only remove some of them. First of all, static method is the method not using "this", having no access to the instance of its class. Please see my past answers for explanations (it does not matter that they answered .NET, and of one the C++ questions; the principles are exactly the same):
Type casting in c#[^],
C# windows base this key word related and its uses in the application[^],
Catch 22 - Pointers to interface objects die when function using them is made static.[^],
What makes static methods accessible?[^].

When two thread access different objects, the threads don't interfere with each other. If you use only static methods and only the objects passed to them as parameters, you can easily guarantee that two threads always work with different object. If some object is shared between tho threads, you can big problem is you don't synchronize the access to it. I hope you don't have to explain it. And best synchronization is no synchronization.

So, if you use an instant method (not-static) method in two different threads, it all depends on what is that "this" object. If you have two calls in different threads to the same method called on the same object, you are working with the same "this", same object, and it does not even matter that you are calling the same method: you can still access the same object if different thread call different instance methods on the same instance. Now, it depends on what you access through this "this". So, your threads clash and may need synchronization, such as locking.

—SA


—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900