2 Replies Latest reply: Sep 26, 2008 8:12 AM by 807589 RSS

    Trying to understand Static methods

    ziggy
      Hi all,

      I am trying to learn a little bit about static methods and hope that someone here can help me understand it better. My understanding of static methods is that they exist only once (More like global methods). This tells me that usage of static methods should be used with care as they are likely to cause problems in cases where multiple users try to access a static object at the same time.

      I am looking at a piece of code that had me thinking for a bit. I cant post the code itself but here is an example of how the code is structured

      The first class declares a couple of non static arrays and makes them available via the getters and setters. It also has one method that calls a static method in another class.
      package com.tests.statictest;
      
      import java.util.ArrayList;
      
      public class ClassA{
           
           ArrayList arrayList1 = new ArrayList();
           ArrayList arrayList2 = new ArrayList();
           
           public ClassA(){
                arrayList1.add("TEST1");
                arrayList1.add("TEST2");
      
                arrayList2.add("Test3");
                arrayList2.add("Test4");
           
           }
           
           ArrayList getArrayList1(){
                return arrayList1;
           }
           
           ArrayList getArrayList2(){
                return arrayList2;
           }
      
           ArrayList getJoinedArrayList(){
                return ClassB.joinArrays(arrayList1,arrayList2);
           }
           
      }
      The second class contains the static method that is called by the above class to join the two arrays. This class is used by many other classes
      package com.tests.statictest;
      
      import java.util.ArrayList;
      
      public class ClassB{
           public static ArrayList joinArrays(ArrayList list1, ArrayList list2){
                ArrayList list3 = new ArrayList();
                list1.addAll(list2);
                return list1;
           }
      }
      And here is my test class
      package com.tests.statictest;
      
      public class ClassC{
            public static void main(String args[]){
                 ClassA classA = new ClassA();
                
                System.out.println(classA.getArrayList1());
                System.out.println(classA.getArrayList2());
                System.out.println(classA.getJoinedArrayList());
            
            }
           }
      The output to the above program is shown below
      [TEST1, TEST2]
      [Test3, Test4]
      [TEST1, TEST2, Test3, Test4]
      My question for the above is that i am wondering if the above is safe. Can you think of situations where the above is not recommended. What exactly would happen if ten instances of ClassA threads are executed at the same time? Woulnt the static method in ClassB corrupt the data?
        • 1. Re: Trying to understand Static methods
          807589
          ziggy wrote:
          Hi all,

          I am trying to learn a little bit about static methods and hope that someone here can help me understand it better. My understanding of static methods is that they exist only once (More like global methods). This tells me that usage of static methods should be used with care as they are likely to cause problems in cases where multiple users try to access a static object at the same time.
          There is no such thing as a "static object" in Java. The word "static" simply means "belonging to a class as a whole, rather than an individual instance."
          My question for the above is that i am wondering if the above is safe. Can you think of situations where the above is not recommended. What exactly would happen if ten instances of ClassA threads are executed at the same time?
          ClassA isn't a thread, so it can't be "executed", per se.
          Woulnt the static method in ClassB corrupt the data?
          "Staticness" doesn't have anything to do with it; the issue at hand is operations on a shared data structure, which is a concern whether you're dealing with static or non-static members. There is nothing inherent in ClassB that will "corrupt" anything, however.

          ~
          • 2. Re: Trying to understand Static methods
            807589
            There are no problems, as such, with methods being executed simultaneously by any number of threads, and local variables and parameters don't present any "thread safety" issues because each thread executing a method has it's own stack frame and thus it's own version of local variable.

            Where are potential problems is with access to the same fields by more than one thread, which is were synchronization may be needed. But it's only in certain circumstances. For example say two threads attempt to add one to the same static int field. You could get the situation where the two increment operations interleave, thread 1 reads the value, thread 2 reads the value, thread one adds 1 to it and stores it, then thread 2. Result is that only on increment is added. This might happen only once in a blue moon, which makes it hard to find in testing.