Results 1 to 5 of 5
- 01-12-2013, 07:47 PM #1
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 676
- Rep Power
- 1
Public Arrays with Private Elements
I recently discovered that one cannot solicit the length of a public array which contains private elements. At least the way I
constructed it. I find this counter-intuitive. The JLS for SE 7.0 Section 10 says that the length parameter of an array is not part of its type. I interpret that to mean independent of its type (which in this case is Foobar[]). It also says that the length attribute of an array is public and final. However, when I uncomment item (1) below I get the shown compile time error. If I remove the access modifier on Foobar or change it to public, the problem goes away. Can someone please explain to me why this is occurring? It seems to me that the length of an array should have nothing to do with the type of the array or the accessibility of its elements. I am presently using Eclipse Junos as my IDE. Note: Presently I have no requirement to do this. I stumbled across it while doing some testing unrelated to this problem. Thanks for the help.
Jim
public class Puzzle {
public static void main(String[] args) {
PublicArray array = new PublicArray();
array.init();
System.out.println(array.table[10]); // prints 10 as expected
// int len = array.table.length; // compiler error -- PublicArray.Foobar is not visible. (1)
}
}
class PublicArray {
public Foobar[] table = new Foobar[100];
private static class Foobar {
public int element;
Foobar(int e) {
this.element = e;
}
public String toString() {
return element+"";
}
}
public void init() {
for (int j = 0; j < table.length; j++) {
table[j] = new Foobar(j);
}
}
}
-
Re: Public Arrays with Private Elements
That's interesting and somewhat counter intuitive, but I'm betting is specified somewhere in the bowels of the JLS. Myself, I make a habit of avoiding use of public fields and of directly querying fields, but rather use mostly all private fields with public getter methods when necessary such as by giving Puzzle a public int getTableLength() method.
Welcome to the java-forums.org by the way!
- 01-12-2013, 09:48 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 676
- Rep Power
- 1
Re: Public Arrays with Private Elements
-
Re: Public Arrays with Private Elements
I couldn't find anything in the JLS, so I had a look at the java.lang.reflect.Array.java, which I think is the source code for all Java arrays, to see how length was obtained, but it appears that the method used, getLength() is a native call.
- 01-12-2013, 11:36 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Public Arrays with Private Elements
I haven't looked that closely at the JLS (eg to find out *exactly* what a type is) but length is not part of an array's type - that is to say (v 10.1) that it plays no part in declarations or casting. But length is a public field of the array and clone() is a public method (10.7). So these things do function as part of "what sort of thing the array is". Certainly the compiler complains "length [/clone()] in Array is defined in an inaccessible class or interface" if you try and access them.
I notice you quote a slightly different compiler message, but I think "<whatever> is defined in an inaccessible class" gets at the heart of the problem. The compiler observes that you are accessing a field defined in a class that is inaccessible and stops you, even though that field - when it comes to declarations or casting - is not considered part of the type. Messy. And part of the general messiness of arrays in Java.
Note that length (and clone()) are both accessible if you cast to Object[], a type which is accessible.
The code would be clearer (and compile- and runtime equivalent ???) if PublicArray had been defined as:Java Code:// len=100 System.out.println("len=" + ((Object[])(array.table)).length); // [LPublicArray$Foobar Object[] asd = ((Object[])(array.table)).clone(); System.out.println(asd);
This sort of thing - "declare it as a publicly known about vanilla type, but construct and work with it as some private subtype" - is a common construct. As Fubarable says a getter method might be used to do the type conversion.Java Code:class PublicArray { public Object[] table = new Foobar[100]; // etcLast edited by pbrockway2; 01-12-2013 at 11:41 PM.
Similar Threads
-
If a and b are both int arrays, will a = b copy all elements of b into a?
By psx2514 in forum New To JavaReplies: 2Last Post: 11-08-2012, 07:54 AM -
store the public/Private keys in Database
By kumar11 in forum Forum LobbyReplies: 1Last Post: 07-03-2012, 02:55 PM -
Difference between public/private/protected inner classes?
By castiel in forum New To JavaReplies: 1Last Post: 02-10-2011, 04:43 AM -
Public, private or (nothing) class
By tyang in forum New To JavaReplies: 3Last Post: 01-31-2010, 11:37 PM -
How to access private data types from public classes?
By kevzspeare in forum New To JavaReplies: 3Last Post: 03-07-2009, 04:19 AM


4Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks