Thursday, October 9, 2008

static context in Java

Hi,

Did you know that this:


passwordBase64Str = org.apache.xerces.impl.dv.util.Base64().encode(password.getBytes());


---> leads to a warning inside Eclipse IDE (Version: 3.4.0 Build, id: I20080617-2000), when you get the underlined (by default) yellow line of your code? It tells about static context, in which you need to access the method encode().

Eclipse suggests to resolve the warning this way:


new org.apache.xerces.impl.dv.util.Base64();
passwordBase64Str = Base64.encode(password.getBytes());


I.e. first, you create an (anonymous?) object of type Base64(). Second, you call its method encode().



Now the tricky point for me was understanding the instruction


new org.apache.xerces.impl.dv.util.Base64();


Is this supposed to create an anonymous class, which will (upon creation) be known as exactly "Base64" inside the JVM?

It is further interesting, that if I substitute Base64() in the original call (in the beginning of this post)


org.apache.xerces.impl.dv.util.Base64().encode(password.getBytes());


for the suggested by the IDE


new org.apache.xerces.impl.dv.util.Base64()


I get:


passwordBase64Str = (new org.apache.xerces.impl.dv.util.Base64()).encode(password.getBytes());


for which, the Eclipse produces the same warning above.

Does it confuse you?



upd: when I turned to explaining the issue second time to one friend of mine, I have realized that from these two lines:


new org.apache.xerces.impl.dv.util.Base64();
passwordBase64Str = Base64.encode(password.getBytes());


the first one is obsolete. So this is just a "bug" (a "feature") in the Eclipse IDE.

No comments: