jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Static method and classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static method and classes" Watch "Static method and classes" New topic

Static method and classes

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I have a JSP which is trying to access one fo my java utility classes. I am using Weblogic 5.1
In my JSP I have the following:
(with page imports, etc.)

Connection conn = JSPUtils.getConnection();
My java class file looks like this:
package com.fedex.util;
import weblogic.db.jdbc.*;
import java.sql.*;
import java.util.*;
import java.text.*;

* A utility class for commonly peformed operations within the JSP pages

public class JSPUtils


private Connection connection;
private String jdbcClass = "weblogic.jdbc.pool.Driver";
private String jdbcURL = "jdbc:weblogic ool ostgresPool";

* Returns a connection to the database

public Connection getConnection()

connection = DriverManager.getConnection(jdbcURL);

catch (Exception e) {

return connection;
But on my jsp i am gettign the error:
non-static method getConnection() cannot be referenced
from a static context.
Why woudl I have to make getConnection() static. I coudl have sworw I have done something like this before w/o having to make anything static.
Is it becasue I have member variables and the last time that I did this I did not?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

This is a pretty basic Java concept.
A reference of type ClassName.myMethod() is a static reference. For it to work, myMethod must be static.
To call instance (non-static) methods, you need an instance of the class. For example:

or some such.
I'm going to shuffle this off to Java in General (beginner) for any further discussion since it's really nothing to do with JSPs.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link: http://aspose.com/file-tools
subject: Static method and classes
It's not a secret anymore!