SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Static problems

Hybrid View

  1. #1
    SitePoint Addict mmanders's Avatar
    Join Date
    Jul 2006
    Location
    Edinburgh, Scotland
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Pittsburgh, Los Angeles
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •