• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessibility. A subtle case

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

I want to share something I found interesting.


In the following code



What is correct:

1- The code compiles successfully and print 3
2- Compile time error at line 6
3- Compile time error at line 7
4- Non of the above


Good luck!

[ September 21, 2005: Message edited by: Bilal Sallakh ]

[ September 22, 2005: Message edited by: Bilal Sallakh ]
[ September 23, 2005: Message edited by: Bilal Sallakh ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure there are no typos in your example?

Did you make up this question yourself? If no, what is the source of the question?
[ September 21, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the code compilation fails at line 6.
"new"??? you cannot create an instance of a class field
or is the some other hitch?

whats the answer anyway? I'm in no mood to type it all and compile
 
Bilal Sallakh
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Are you sure there are no typos in your example?

Did you make up this question yourself? If no, what is the source of the question?



That's right Barry, thank you. I corrected the error.

There is no source for the question. I just came across this situation and found it a bit meaningful to share.
 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Bilal

i didnot get what you are saying ?
 
Bilal Sallakh
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karthik Rajashekaran:
hi Bilal

i didnot get what you are saying ?



I'm sorry if the question is not clear enough.

Just try to answer the question, or you can try to compile the code.

What exactly do you find unclear?
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would be haappy if there could be an explanation for this..



Why???

Regards
 
Bilal Sallakh
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is what I wante to share with you. There is a nice justification for it, and I wanted you to search for it
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the original question at the top of the thread:

C2 is package private to package p1, so it is not visible to members of class Question in package p2.
[ September 23, 2005: Message edited by: Barry Gaunt ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In that case the first pritnln statement should show a compilation error..

Why isnt that??

Regards
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because C1 is public

to make this work, place C2 in another file C2.java in the same package and make it public
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by A Kumar:
Hi,

In that case the first pritnln statement should show a compilation error..

Why isnt that??

Regards



I'm answering the original question at the top of the thread
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by A Kumar:
Hi,

In that case the first pritnln statement should show a compilation error..

Why isnt that??

Regards



For this question:

p1.C1.arr is a public member arr of a public class C1 of the imported package p1. So o.arr is visible to class Question.main.

Insert new p1.C2(); into the main and you will see that C2 is not visible.
[ September 23, 2005: Message edited by: Barry Gaunt ]
 
Bilal Sallakh
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akshay Kiran:
because C1 is public

to make this work, place C2 in another file C2.java in the same package and make it public



The fact that C1 is public has nothing to do with the question.

Kumar wanted to say: the code fails to compile at line 7 because C2 is inaccessible. Why didn't it fail to compile earlier a line 6 for the same reason?

Actually, that was the point I wanted to show
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi barry,

But still i have a doubt...(Please refer to the code I added)

if the code in Question is able to access o.arr...

which means there is an array...constructed..

and this is member of C1 (public class)

So the println statement of arr.length should be fine..

Then why not..??

Regards

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by A Kumar:
Hi barry,

But still i have a doubt...(Please refer to the code I added)

if the code in Question is able to access o.arr...

which means there is an array...constructed..

and this is member of C1 (public class)

So the println statement of arr.length should be fine..

Then why not..??

Regards




The construction of the array has nothing to do with it (at compile-time). You can remove the assignment to arr of new C2[3]. You will get a problem at runtime however.

It's only when class Question needs to know the type of arr you get the compile-time error. For example (in main):

Object[] a = o.arr; compiles
p1.C2[] c = o.arr; does not compile

To get the length of the array arr the compiler needs to know what type reference variable arr is (it's an array of C2), but the compiler cannot find out the type of arr because C2 is not visible.

If you put in:
int len = o.arr.length; you will get a compile-time error of C2 not visible.

You would think that the compiler would only need to know that o.arr is an array, no more than that, because every array has an int length member. But to ensure type-safeness the compiler also checks the type of the array elements.
[ September 23, 2005: Message edited by: Barry Gaunt ]
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the line in bold which uses a reference of C1, C1 as you can see, is public.
And as you see, the reference to the array of type C2 is visible, but when you need to access its property, it needs to be available publicly, but since the class C2 has just default accessibility, it is not visible and gives you an error,
doing what I say will confirm the truth, and anyway, I still don't get what point you're trying to emphasise sorry, I'm not very good at reading minds
[ September 23, 2005: Message edited by: Akshay Kiran ]
 
Bilal Sallakh
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akshay, I'm sorry I didn't mean to offend.

Barry, thanks for your clarification

It's true that you can't use o.arr.length directly, but you can still know the size of the arry using ((Object[])(o.arr)).length
I think this is one of the benefits of the Hierarchy C2[] is Object[] is Object, rather than the hierarchy C2[] is Object.
But it can cause something called ArrayStoreException if the programmer is not careful enough. Java is good but it's not that good (Bruce Eckel 2002) . Yet, I find it the best
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bilal Sallakh:

It's true that you can't use o.arr.length directly, but you can still know the size of the arry using ((Object[])(o.arr)).length



The cast to (Object[]) is a good point to make. In effect you are saying to the compiler, "yes I know all about that type-safety stuff but I know what I'm doing - it's an array and I just want to know its length".
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Second: Bilal, there was nothing to be offended about buddy! thanks for the concern anyway

First: Yes, I clearly see the "subtlety" of your point, it is indeed a very fine point to make, I agree, and thank you for sharing it. I was unaware of it, and now, am one step closer to enlightenment

have a nice day!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic