Re: Oddities in Cocoa/Java object initialization?
Re: Oddities in Cocoa/Java object initialization?
- Subject: Re: Oddities in Cocoa/Java object initialization?
- From: Joe Letteri <email@hidden>
- Date: Fri, 29 Jun 2001 08:24:07 -0700
>
I have a Java sub-class (twice removed) of NSDocument that seems to have
>
two unexpected initialization behaviors (at least I didn't expect them):
>
>
1) Two constructors -- the no-arg constructor and the two arg (filename
>
and type) seem to be getting called when the document is constructed;
>
The behavior in Cocoa Java seems to be that the no-arg constructor always
gets invoked immediately by the other constructors, which must return before
the body of the calling constructor gets invoked. So if you have:
public MyDocument()
{
super(); // This gets called second
int x = 1; // This gets called third
}
public MyDocument(String fileName, String fileType)
{
super(fileName, fileType); // This gets called first
int x = 2; // This gets called fourth
}
>
2) A private data member (a boolean flag that's supposed to be
>
initialized to "false") is getting "initialized" to false *after* the
>
constructor(s) is(are) executed. This seems backward from the normal
>
behavior of Java classes.
My solution has been to set up the constructors with a single initialize
method that gets called from the no-arg constructor:
boolean flag;
public MyDocument()
{
super();
initialize();
}
public MyDocument(String fileName, String fileType)
{
super(fileName, fileType);
}
public void initialize() { flag = false; }
>
>
Are these behaviors perhaps a consequence of inheriting from Objective-C
>
classes?
>
I suspect that this is because the no-arg constructor gets mapped to an init
method in ObjC. Also, I don't know if the same thing happens without the
super(fileName, fileType) call.
>
Thanks in advance...
>
>
Doug Knowles
>
_______________________________________________
>
cocoa-dev mailing list
>
email@hidden
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev