-
Static problems
Hi,
I have two classes, each in a different package. One class, DBConnector implements the Singleton design pattern so as to return only a single object. The other class extends HttpServlet. I am trying to call DBConnector.getConnection() from my servlet class, but I get the error message:
"Cannot access non-static method from static context"
The problem is, neither class uses the static keyword and I can't see why the servlet would be considered a static context.
Any suggestions appreciated.
-
In order to call this:
Code:
DBConnector.getConnection()
You need to have a static method declared in DBConnector:
Code:
public static Connection getConnection() {
....
}
Also when using the singleton pattern, its usually best to do something like this:
Code:
public static DBConnector getInstance() {
return INSTANCE;
}
Where INSTACE is constructed via a private consturctor.