Now blogging at diego's weblog. See you over there!

protected does what?

Okay, quick, what's the effect of adding the protected qualifier to a class variable in Java?

If you're like me (and many others) you'd say: "protected allows only subclasses to access the variable".

Wrong!

At least according to Java 1.4 and Java 5...

We discovered this a few months back while chatting with Erik and I forgot to blog about it. Today I remembered it in a conversation with Martin.

Let's look at the Java Language Spec section for protected access, 6.6.2.1 "Access to a protected Member", it states:

"Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C."
Hm. That sounds like only subclasses should be able to access it, doesn't it. But try this code. Create package "x". Then create two classes in there, test1 and test2, with the following contents:

test1.java

package x;

public class test1
{
protected String hello = "TEST";
}

test2.java
package x;

public class test2
{
public static void main(String[] args){
test1 xx = new test1();
System.out.println(xx.hello);
}
}

Not only that will compile, but also running java x.test2 will print "HELLO". Which means that protected is giving package-wide access, not just subclass access. If you read the first definition in the spec:
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
It seems to be loose in specifying only access-denied for non-package classes that do not inherit (a "negative right"), rather than only for inheriting classes (a "positive right", or is it the other way around? nevermind).

Okay, so what do you think? Does the Java Lang Spec contradict itself by not spec'ing out completely access in the first paragraph? Or is section 6.6.2.1 enough to say that this is a bug? (And I have confirmed this behavior in JDK 1.4.x and Java 5 as well).

Update: Juha, in the comments, notes that section 6.6.1 (a little before the section I referenced) makes the package-distinction clear. Martin agrees. Me? I agree that putting sections 6.6.1 together with 6.6.2 gives us the behavior we're seeing. I might be wrong in reading the text like I am (eu mentioned that in the comments), but I can't quite see it that way. I still think that Section 6.6.2 reads as if it's an all-inclusive rule, particularly since it doesn't reference section 6.6.1, only two paragraphs above, and thus sec. 6.6.2 appears to contradict both the spec from Section 6.6.1 and language behavior.

In any case, there's no mystery. At most, I'd say that section 6.6.2 has to be clarified to avoid appearing to contradict 6.6.1. Or rather, the whole section might be rewritten for clarity...

Or, I should change the grammar/parser in my head and read the spec differently (admit it, that's what you think we really needed, don't you!). Then all problems would be solved. I'll see if I can call the factory and get an upgrade or something. :-)

Categories: soft.dev
Posted by diego on January 29, 2005 at 7:57 PM

another microsoft gem

I read this first over at the Wall Street Journal but then found a CNET article on it.

Last year Microsoft actually lost a court case, against the EU, for predatory practices (sounds like something from the Discovery Channel, doesn't it?) in the market for media players/formats. Essentially the EU forced Microsoft to distribute Windows without forcing Windows Media Player to be bundled with it, essentially allowing customers to choose.

So what did MS do? It said "fine". Then it went and called this product "Windows XP Reduced Media Edition". Then it also removed the ability to do some basic things, such as playing music CDs (something that even Windows 95 could do).

If you are a consumer, and are offered, for the same price, a machine with "Windows XP Home Edition" and another with "Windows XP Reduced Media Edition", which one are you gonna get? I can hear the Shadow-like laugh in the background when the MS representative says (in the article) "We believe this name complies with the commission's orders".

You know, it's stuff like this that pisses people off about Microsoft. Maybe you could argue that it's MS's "Darwinian" attitude that allows it to post record profits and market gains, even when most in the tech industry consider it passé.

But isn't there a point when MS just has to say, "okay guys, we've misbehaved, how do we make things better?" instead of fighting tooth and nail for every single tiny scrap of whatever. I have stated more than once here that Microsoft's desktop monopoly is not necessarily illegal in itself, it's the predatory practices and the illegal actions taken to maintain and defend that monopoly that are the problem (sadly, it's also in question whether the monopology would have gotten this far without those illegal actions). Maybe if MS started to differentiate the monopoly itself and what they do to maintain it, then things would get better, don't you think?

No, I'm not holding my breath. But hey, it's Saturday morning. I thought it was Friday. Let's give the lad some leeway.

Bonus: a post from November 2003, where I wonder if WinFS will be delayed again ("never say never" eh?), some more day-dreaming about MS embracing, or at least not actively attacking, the Web (sure), and some Descartes thrown in for good measure.

Categories: technology
Posted by diego on January 29, 2005 at 12:55 PM

spamassassin and comment spam

I have the blog set up (as most others I suspect) to email me when a new comment is pending for moderation. Main reason for this being, of course, comment spam. Now I get lots of comment spam, but it's easy to ignore.

But I also have spamassassin set up to dump any email that scores 5 or higher (I think). And SA learns. Put two and two together and it turns out that after a while SA apparently came to the conclusion that any email that came from comments on the weblog was spam. Now I don't get notified of any comments at all. I'll have to dig into the SA config and figure out which rule is triggering this, or how to override it. I'm not even sure how it's separating them, since the trackback notifications are still arriving, and they have a similar message pattern.

You know, I'd be pissed at SA if it wasn't that it's pretty much on the mark in its decision. Hopefully eventually we'll have SA connected to something like Cyc so that it knows a bit more about the world. Now wouldn't that be a treat.

Categories: technology
Posted by diego on January 29, 2005 at 12:41 PM

today is saturday?!?

So I'm on the phone with a friend this morning and I'm asking if he's ditching work today (since he's telling me how he's going to the beach with wife & family and all) and he laughs quietly. "No, I'm not working today." A few confused moments later and it turns out that this is reasonable, you know, because it's Saturday!

Damn. So I tell him how yesterday night I had ordered some stuff through the internet from Tesco (big items that I can't carry easily, paper towels, that sort of thing) and how I was pissed at them because they had only set a Saturday delivery date for me. I spent the entire day yesterday absolutely convinced that it was Thursday. And this entire morning convinced that it was Friday. Plus today I was even more confused since I went to sleep at 2 am and woke up at 5 , then unable to sleep just got to work and chat with Russ, who was recovering his system from an attack yesterday morning (and hopefully he'll be back up soon).

Then after some well-deserved heckling my friend says, "well, you should know the date because of your blog. You post there after all." But then, in one of those moments when the words are out of your mouth before you know what you're saying, I reply: "my blog knows the what day it is. *I* don't."

We depend on software do we not. And there's the extent to which it is true.

Plus: if anyone can tell me what happened to last week, I'd be grateful. January, too.

Categories: personal
Posted by diego on January 29, 2005 at 12:23 PM

Copyright © Diego Doval 2002-2011.