Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access static methods of a class by multiple object instances simultaneously?

Tags:

java

Example:

ThisClass.staticMethod(Object... parameters);

will be accessed by multiple instances of other objects and simultaneously.

Will there be any dependencies with this other objects when they are using the same static method at the same time?

like image 472
Richeve Bebedor Avatar asked May 30 '10 13:05

Richeve Bebedor


People also ask

Can you have multiple instances of a static class?

A static member has only one copy of the member, regardless of the number of instances. Static members and their values belong to the type itself, rather than the object. If multiple instances of a class are created, the last updated value of a static member will be available to all instances.

Can static method be accessed by multiple threads?

accessing the code is no problem, static methods can be called with multiple threads. It depends on how it is programmed in the method, if the code is not thread safe, it will cause problems.

Can we call static method multiple times?

That depends if the calls are made from different threads. If one thread then sequentially and with more it could run concurrently and in that case synchronization or something alike might be needed.

Can instances access static methods?

Instance method can access static variables and static methods directly. Static methods can access the static variables and static methods directly. Static methods can't access instance methods and instance variables directly. They must use reference to object.


3 Answers

Only if the method uses static Objects or the arguments are shared by the other instances.

Example: Math.max(int a, int b) is a static method but does not use any static objects, so there are no dependencies.

Example 2: Here all invocations share the same result variable, two parallel calls to staticMethod can cause wrong results.

private static int result = 0;
private static int staticMethod(Object... args)
{
      result = args.length;
      //Do Something
      return result;
}

Example 3: This one is Thread safe if none of the arguments are shared, each invocation has its own instance of result.

private static int staticMethod(Object... args)
{
    int result = 0;
    result = args.length;
    //Do something
    return result;
}

Example 4: This uses the class as a lock to prevent parallel access to the class functions. Only one call to staticMethod executes all others wait

private static int result = 0;
private static synchronized int staticMethod(Object... args)
{
      result = args.length;
      //Do Something
      return result;
}
like image 151
josefx Avatar answered Oct 19 '22 23:10

josefx


A code fragment running concurrently by multiple threads has the potential of causing race conditions, regardless of where this code is placed (static/non-static method).

You must make sure that the data manipulated by the code tolerates concurrent accesses, and synchronize the code properly if needed.

like image 25
Eyal Schneider Avatar answered Oct 20 '22 00:10

Eyal Schneider


Obviously, all users of the method depend on the class, if this is what you mean.

Moreover, if they are really calling the method simultaneously (i.e. from multiple threads), you may get concurrency problems, depending on what the method actually does. Show us the code, and we may be able to tell more.

like image 30
Péter Török Avatar answered Oct 20 '22 00:10

Péter Török