Results 1 to 5 of 5
Like Tree4Likes
  • 4 Post By pbrockway2

Thread: Public Arrays with Private Elements

  1. #1
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default 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
    Java Code:
    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);
    	  }
       }
    }
    Last edited by DarrylBurke; 06-28-2013 at 04:20 PM. Reason: Added code tags on request

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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!

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Public Arrays with Private Elements

    Quote Originally Posted by Fubarable View Post
    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!
    Thanks! I agree entirely. I almost always use getters and setters and make my instance fields private. This was something that I just threw together to facilitate a test. I would really like to find the JLS paragraph that explains this.

    v/r.
    Jim

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

    Java Code:
        // len=100
    System.out.println("len=" + ((Object[])(array.table)).length);
    
        // [LPublicArray$Foobar
    Object[] asd = ((Object[])(array.table)).clone();
    System.out.println(asd);
    The code would be clearer (and compile- and runtime equivalent ???) if PublicArray had been defined as:

    Java Code:
    class PublicArray {
        public Object[] table = new Foobar[100];
    
        // etc
    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.
    Last edited by pbrockway2; 01-12-2013 at 11:41 PM.

Similar Threads

  1. Replies: 2
    Last Post: 11-08-2012, 07:54 AM
  2. store the public/Private keys in Database
    By kumar11 in forum Forum Lobby
    Replies: 1
    Last Post: 07-03-2012, 02:55 PM
  3. Replies: 1
    Last Post: 02-10-2011, 04:43 AM
  4. Public, private or (nothing) class
    By tyang in forum New To Java
    Replies: 3
    Last Post: 01-31-2010, 11:37 PM
  5. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •