Article View: borland.public.jbuilder.java.language
Article #17865Re: Implementing the Parameter Constants
From: John P. McGrath
Date: Mon, 14 Sep 1998 00:00
Date: Mon, 14 Sep 1998 00:00
128 lines
4856 bytes
4856 bytes
Grant Sayer <g.sayer@unsw.edu.au> wrote:
I think there is a compiler bug involved here, or at least an very
inappropriate error message. But try declaring the WeekDay class
"static". That will probably fix your problem.
When you declare a non-static inner class, each instance of the inner
class is associated with an instance of the outer class. The Java
compiler adds an additional member to the class, which is initialized to
point to the associated instance of the outer class. This allows methods
in the inner to class to reference members and methods in the outer
class. For example:
class A {
String name;
A( String name ) {
this.name = name;
B b = new B();
b.print();
}
class B {
print() {
System.out.println("This is a B in " + name);
}
}
}
B.print() references the member "name" from the outer class. What the
compiler has done is implicitly defined a new member in class B, of type
"A" (i.e. a reference to an A). Each B constructor is also implicitly
given an additional argument of type A. In the constructor it
initializes the reference to the value of the implicit argument. This is
called the "enclosing class instance". When you reference "name" in the
B inner class, the compiler implicitly generates code that uses this
reference.
If you construct an instance of the inner class from within a non-static
context of the outer class, it implicitly supplies the reference to the
outer class. In any other context, you must supply the reference to an
outer class instance, using code something like the following:
A a = new A();
B b = a.new B(); // creates an instance of B linked to a.
You may also explicitly use this member from within the inner class,
using the syntax: B.this. But in the class file, the member is named
"this$0". This was done to maintain compatibility with Java 1.0.
If you declare an inner class "static", then the instances of the inner
class are not associated with an instance of the outer class. The this$0
pointer does not exist, and you may not reference outer class members
from within the inner class.
In your example, the Weekday class is not declared static, but I think
that is what you want. You do not want the WeekDay objects to be
associated with a particular instance of a ConTest object. So you should
declare the WeekDay class as static.
The error in your code is indeed on the line that the compiler indicates,
but the message it gives is pretty cryptic:
> public interface Parameters {
> WeekDay SUNDAY = new WeekDay(0);
As you probably know, all members declared in an interface are implicitly
static (and public and final as well). So the initializers for them are
called outside of the context of a particular instance. This means that
the compiler does not have a reference to a ConTest object to pass to the
WeekDay constructor. This is why it was saying that this$0 was not
initialized.
--
Regards,
John P. McGrath
<mcgrath@enter.net>
<jpm@nscs.fast.net>
> Hi
>
> I've got a language question when attempting to implement the Parameter
> Constants pattern as written in a javaworld article (see:
> http://www.javaworld.com/javaworld/javatips/jw-javatip50.html).
> Basically this pattern is a way of providing type safety for constants
> defined in a class. A sample implementation is shown below:
>
> public class ConTest implements ConTest.Parameters {
>
> public interface Parameters {
> WeekDay SUNDAY = new WeekDay(0);
>
> final class WeekDay {
> private final int weekday;
>
> private WeekDay(int i){
> weekday = i;
> }
> }
> }
>
> public ConTest(Parameters.WeekDay weekday){
> }
>
>
> public static void main(String[] args){
> ConTest con = new ConTest(ConTest.SUNDAY);
> }
> }
>
> The problem is that when implemented in JBuilder I get a compilation
> error for the line WeekDay SUNDAY = new WeekDay(0); whereby the
> compiler complains
>
> "variable this$0 might not have been initialized"
>
> Yet in comparison, compiling & running with JDK 1.1.6 there are no
> problems.
>
> Is this a bug in the compiler for JBuilder (version 2.0) ?
>
> Any comments are most welcome
>
> Grant
>
>
>
Technical Details
Message-ID:
<MPG.1066964fc948446c989698@forums.inprise.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<35F4F62C.188F6984@unsw.edu.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,dbee29e333f0dc17 X-Google-Attributes: gidf74b3,public From: mcgrath@enter.net (John P. McGrath) Subject: Re: Implementing the Parameter Constants Date: 1998/09/14 Message-ID: <MPG.1066964fc948446c989698@forums.inprise.com> X-Deja-AN: 390918256 References: <35F4F62C.188F6984@unsw.edu.au> Organization: Software Consulting Services Newsgroups: borland.public.jbuilder.java.language