Actually, there is another error as well. The code in an initializer is not
in a truly static context unless it is preceded by the "static" keyword.

Example:

public class AClass {
  public static int foo;
  static {
    // will assign foo to 2 upon class loading
    foo = 2;
  }

  public int bar;

  {
    // will assign bar to 2 for each new instance.
    bar = 2;
  }
}

This can make things really confusing when instantiating classes with this
kind of thing. If you're really interested I suggest creating a class (and a
superclass) with this kind of structure and then stepping through it in a
debugger.

(BTW, we're getting pretty far away from servlets at this point.)

~Mike

----- Original Message -----
From: "Colin Capriati" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, December 28, 2001 8:37 PM
Subject: Re: Static Reference Issue


> I apologize there is an error below; the code within a constructor is NOT
> static.
>
> Sorry,
>
> Colin
>
> ----- Original Message -----
> From: "Colin Capriati" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Friday, December 28, 2001 6:28 PM
> Subject: Re: Static Reference Issue
>
>
> > Static context is from within a method declared as static:
> >
> > public static void aMethod()
> > {
> >     // static context
> > }
> >
> > Or from within a constructor:
> >
> > public Class Aclass
> > {
> >     public Aclass()
> >     {
> >     // static context
> >     }
> > }
> >
> > Or from within a static code block (excecuted at Class load time)
> >
> > public Class Aclass
> > {
> >     {
> >     // the code in this block is static and will be executed when Aclass
> is
> > loaded
> >     }
> > }
> >
> > The context of a try block is that of it's surrounding block, a try
block
> is
> > not inherently static or non-static.
> >
> > The error you are getting is due to the fact that you are qualifying the
> > method setLastAccess() with the Class UserBean. First instantiate
UserBean
> > then call the method on that instance.
> >
> > Hope this helps,
> >
> > Colin
> >
> > ----- Original Message -----
> > From: "Mark Galbreath" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Friday, December 28, 2001 7:13 AM
> > Subject: Static Reference Issue
> >
> >
> > > I know I've run across this before, but can't remember the
circumstance.
> > > When I attempt to set a JavaBean property from a servlet as so:
> > >
> > > try {
> > >     ...
> > >     UserBean.setLastAccess( rs.getString( "lastaccess"));
> > >     ...
> > > }
> > >
> > > with the bean method as:
> > >
> > > public void setLastAccess( String lastaccess) {
> > >     this.lastaccess = lastaccess;
> > > }
> > >
> > > I get a compile-time error:
> > >
> > > "non-static method setLastAccess( java.lang.String) cannot be accessed
> > from
> > > a static context."
> > >
> > > Is a <try> block considered a static context?
> > >
> > > ~Mark
> > >
> > >
> >
>
___________________________________________________________________________
> > > To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> > body
> > > of the message "signoff SERVLET-INTEREST".
> > >
> > > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > > Resources:
http://java.sun.com/products/servlet/external-resources.html
> > > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> > >
> > >
> >
> >
>
___________________________________________________________________________
> > To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> body
> > of the message "signoff SERVLET-INTEREST".
> >
> > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > Resources: http://java.sun.com/products/servlet/external-resources.html
> > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> >
> >
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to