Calling static methods from the web tier

Discussions

General J2EE: Calling static methods from the web tier

  1. Calling static methods from the web tier (7 messages)

    I've gotten used to the following paradigm, and I just wanted to get another opinion as to if it's safe or not:

    Let's say I have a controller (myController) in the web tier of my app, that calls a static method on a generic utility class (myUtilClass) that in turn calls a stored procedure. Assuming one JVM here. So:

    public class myController {
      ...
      myUtilClass.deleteTrans(id);
    }

    public class myUtilClass {
      
       public static void deleteTrans(String id) throws Exception {
            //call stored proc "deleteTrans"+idx;
       }
    }

    Is this safe to do? Or is it possible that my input value could get clobbered by a subsequent call to this method before the first one completes? I'd rather not put the 'synchronized' keyword in front of the static method if I do not have to.

    Threaded Messages (7)

  2. I don't have a definitive answer but I have always avoided static methods because they are a system bottleneck. If a method is static there is only one instance of that method in the JVM. Every call to that method is goes through the one instance. If it is not synchronized I think there could be thread trouble. Synchronizing the method eliminates the potential threading problem but creates the bottleneck.

    Someone please correct me if I'm wrong.
  3. Static method should only be used for stateless, non-synchronized situation
    and also, the static calss/method should not need any initialization

    if can not meet the condition above, I would use instance level method. - after all, now object instantiation is not that expensive, and you can cache your object.

    Static method should be a place only be used for hold a collection of related stateless functions.
  4. Static methods a bottleneck?[ Go to top ]

    ... I have always avoided static methods because they are a system bottleneck. If a method is static there is only one instance of that method in the JVM. Every call to that method is goes through the one instance.
    I've heard that several times and I think it's plain wrong. Of course, there's only one "instance" of the method, but the same is true for real instance (object-level) methods: they simply have an additional unvisible "this" parameter that identifies the object instance they are applied to. So, as long as a method is stateless, a static method is actually slightly faster, since it is faster to call (no virtual call, no additional "this" parameter on the stack frame).

    Correct me, if I'm wrong:)

    Kind regards,
    Stefan
  5. Static methods a bottleneck?[ Go to top ]

    ... I have always avoided static methods because they are a system bottleneck. If a method is static there is only one instance of that method in the JVM. Every call to that method is goes through the one instance.
    I've heard that several times and I think it's plain wrong. Of course, there's only one "instance" of the method, but the same is true for real instance (object-level) methods: they simply have an additional unvisible "this" parameter that identifies the object instance they are applied to. So, as long as a method is stateless, a static method is actually slightly faster, since it is faster to call (no virtual call, no additional "this" parameter on the stack frame).Correct me, if I'm wrong:)Kind regards,Stefan
    Well, I'm not commenting on whether a static method is any faster or slower than a non-static method. I don't know. I agree that a syncronized static method could be fast. But it seems to me that if several threads were lined up waiting for a turn, it could become a bottleneck and slow the system down.
    Who can answer this?
  6. Static methods a bottleneck?[ Go to top ]

    Synchronizing the method eliminates the potential threading problem but creates the bottleneck.
    But it seems to me that if several threads were lined up waiting for a turn, it could become a bottleneck and slow the system down.
    Ok, I got it. I didn't read you original post as accurately as possible. Sorry :)

    Kind regards,
    Stefan
  7. Your String id is put onto the stack when the method is executed. Any other call to the same method would also put their value on the stack. The two stacks do not overlap, and a static method can use any parameters without worrying.

    I agree with Steven, but make sure that you still seperate the logic from your controller.

    Hope that helps.
  8. No problem[ Go to top ]

    As rightly said, any function call has its own variable instances.. doesn't matter if its staitic or not, its only the static class member variables which would have such a problem of being shared between multiple function calls... and regarding the consideration mentioned about using it in the first place, I too agree to that, use it only if you need it that way.

    Hope this helps.

    Regards,
    Rakesh.