protected constructor confusion

Hi All

Please see the following code


new C1(); is an error. But IDEA is not showing any error and there is Green light.

Is this a bug in IDEA? Or Am I missing something?

Thanks
Anki.N

9 comments

Forgot to mention --- Build is 944

0


Why would this code be invalid? C2 extends C1 and thus has access to all protected members (including constructors).

0

Thats why confusing. Compile this class, it gives error.

Is still access modifiers not stabilized in JLS???

0

By the way JDK is 1.3.0_03

0

Imagine you have a protected method

in C1.
It's ok to call

from a method in C2. However it's
not allowed to call foo on an another object of class C1:

<-- error

That's why you can call

from within C2's constructor but cannot usr

.

So it's a bug in Idea, please file a tracker item (btw the bug is still there in 977).

0

Please submit an SCR.

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"

"Anki" <no_mail@jetbrains.com> wrote in message
news:3423438.1070577704896.JavaMail.javamailuser@localhost...

Hi All

>

Please see the following code

 package p1;
>
> public class C1 {
>     protected C1() { } //protected constructor
> }
>
> --------------
>
> package p2;
> import p1.*;
>
> public class C2 extends C1 {
>     public static void amethod() {
>         new C1(); // <<<     }
> }
> ]]>

>

new C1(); is an error. But IDEA is not showing any error and there is

Green light.
>

Is this a bug in IDEA? Or Am I missing something?

>

Thanks
Anki.N



0

The protected constructors is not easy.
http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#72728

You can call protected constructors by "new" operators only in case of inner class, as i can understand

Thanks!

0

You learn something new every day. Thanks.

--Dave

0

Please sign in to leave a comment.