2

Here is an example:

int[] a = {10};
int x = a.length;

We read incessantly to hide variables and use accessors. Why was that rule violated here?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
  • 2
    `"Why has java kept length field in the jvm public"` -- So lets make it private and thereby break 80-90% of all Java code in existence. Um.... how about no. As to why it was initially made that way -- you'll have to ask the creators of Java. – Hovercraft Full Of Eels Jun 18 '15 at 16:55
  • What is the worst that can happen if it's `public`? _rule_ isn't the same as _convention_. – Sotirios Delimanolis Jun 18 '15 at 16:55
  • http://stackoverflow.com/questions/24563975/java-why-array-length-has-no-but-string-length-does – Sotirios Delimanolis Jun 18 '15 at 16:58
  • 1
    "Why was that rule violated here?" this rule applies to non final/constant fields where we could break encapsulation by allowing changing state in uncontrolled way. In case of final/constant fields there is no need for that. (only disadvantage is that we can't add new behaviour which would be possible with `length()`) – Pshemo Jun 18 '15 at 17:04
  • @SotiriosDelimanolis I like how the reference is marked as a duplicate of a question that is marked as a duplicate of two other questions. I think I see a pattern... ;) Would that be transitive duplication? – MadConan Jun 18 '15 at 17:05
  • 1
    [I explain why the property exists in this answer.](http://stackoverflow.com/questions/23730092/how-can-i-get-the-size-of-an-array-a-collection-or-a-string-in-java) – Makoto Jun 18 '15 at 17:16

3 Answers3

8

A bigger question is; why is there no class file for int[]? Without such a class file there is no where to put such a method, so it is purely a language feature. It looks like a field of int[] but it not. They could have made it look like a method call, but they didn't as it is an immutable field.

If you run this

public static void main(String[] args) throws NoSuchFieldException {
    Field length = args.getClass().getDeclaredField("length");

it throws a NoSuchFieldException as it is not a field like those in a class but rather it is made to look like one.

We read incessantly to hide variables and use accessors.

It is not always necessary and in this case, there would be not benefit, only improve consistency.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

According to Java documentation, length of an array is a final field. This means that no one can modify it, so there is no point in providing a setter for it.

Now since no one can modify it, there is no point for making that field private and providing a getter for it. A getter function will again be loaded into function stack in RAM memory, each time it will be called to get the size of an array. So, to increase performance, it makes sense to just make 'length' of an array as a public field.

On the other hand, a 'size()' method is provided in Collections class. This makes sense, because internal methods can allocate more memory and change the size of Collections.

We do not incessantly hide variables and use accessors. If it makes sense to provide a getter/setter, only then we should make a field as private and provide getter/setter for that.

zookastos
  • 917
  • 10
  • 37
0

One of the possible setback of using accessor is that if one day the return type of the accessor needs to be changed. All codes using the accessor method will encounter problems.

class MyClass{
    private int val;

    public int getVal(){
        return val;
    }
}

Imagine one day the return type of getVal() needs to be changed (e.g. to long). This is one possible problem for all programs which uses getVal() because all along they have been expecting it to be of type int.

user3437460
  • 17,253
  • 15
  • 58
  • 106