blue-sky
blue-sky

Reputation: 53816

Why use a "static" class in this way?

I have a FutureContent class which just holds a static reference to a Future class. I'm not sure why it was done this way, it means every method call now does not have to be static. Is there another reason it was done this way ?

//One possible use of Future class
    FutureContent.future.deleteContent("test");

    public class FutureContent {
            public static Future future = new Future();
    }


public class Future{ 
      private final Object lock = new Object();

      private String str;

      private Hashtable content = new Hashtable();

      public void addContent(Object key, Object value){

          synchronized(lock){
              if(content.containsKey(key)){
                  content.remove(key);
                  content.put(key, value);
              }
              else {
                  content.put(key, value);
              }
          }
      }

      public void clearContent(){
          content.clear();
      }

      public void deleteContent(Object key){
              if(content.containsKey(key)){
                  content.remove(key);
              }
      }

      public boolean getBoolean(Object key){
          if(!content.contains(key)){
              return false;
          }
          else {
              return ((Boolean)content.get(key)).booleanValue();
          }
      }

      public void addBoolean(Object key , boolean value){

          Boolean b = new Boolean(value);

          synchronized(lock){
              if(content.containsKey(key)){
                  content.remove(key);
                  content.put(key, b);
              }
              else {
                  content.put(key, b);
              }
          }
      }

      public Object getContent(Object key){
              return content.get(key);
      }

      public void setString(String str){
          synchronized(lock){
              this.str = str;
              lock.notifyAll();  
          }

      }

      public String getString(){
             synchronized(lock){
                  while(this.str == null)
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        return null;
                    }
                    return this.str;
             }
    }


      private JSONObject value;
      public void set(JSONObject t){
          synchronized(lock){
              value = t;
              lock.notifyAll();  
          }
      }

      public JSONObject get(){
         synchronized(lock){
              while(value == null)
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    return null;
                }

              return value;
         }

      }    
    }

Upvotes: 0

Views: 125

Answers (2)

Johan Sjöberg
Johan Sjöberg

Reputation: 49187

It's an approach to provide a pre-allocated object which is typically immutable and can be shared across instances. A common example is Collections.EMPTY_LIST with the signature

public static final List EMPTY_LIST = new EmptyList<Object>();

Upvotes: 2

Jomoos
Jomoos

Reputation: 13083

It is a way to use globals in Java. Although, we cannot directly create global variables in Java, we can achieve almost the same effect with public and static modifiers. Although, there are many disadvantages to globals, it has got some advantages also. We can easily exchange the data from one place, and that will be reflected everywhere. ie., You can store and access common data between different modules and classes, without any hurdles of passing data between them. But beware! -:)

Upvotes: 1

Related Questions