• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception to the rule: don't declare member variables in servlets?  RSS feed

 
Oliver Chua
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way to make an abstract class with a static abstract method.
To get around this, I defined an abstract class that has to be extended by an implementation, and the implementation class has to be instantiated before calling its method.

I defined the implementation as a static member variable of a Facade class that is called by servlet controller.

I've heard of the maxim don't declare member variables in servlets or in classes that servlets call, but I think this is thread safe because the MyAbstract class has no member variable, just local variables, and local variables are stored in their own heap, meaning there is a separate copy per thread.

Am I right?

public abstract class MyAbstract{
public void myAbstractMethod(StringBuffer message);
public void myMethod(){
StringBuffer localVar="";
myAbstractMethod(localVar);
}
}


public class MyImpl extends MyAbstract{
public void myAbstractMethod(StringBuffer message){
//...
}
}

public class MyFacade{
private static MyImpl m=new MyImpl();

public static void facadeMethod(){
m.myMethod();
}

}
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well since you have not declared any member variables, what's your question?

I am concerned about the static variable though. If the whole JVM is reinstantiated you will get a new static variable, so I wouldn't expect that variable to be shared between instances since instances can be in different JVM, AFAIK.

Perhaps you want to ask in a different forum like the one on servlets and such.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, this has nothing to do with threads that I can see. Moving to the Servlets forum.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!