How java stores array length safely.
Quite recently, in my Object Oriented Programming class, we learnt what constitutes good software design and what doesn’t. Essentially, good software design comes down to how well you’ve documented your code, but there certainly are programming practices that you can adopt which are considered to be “good”.
To keep things simple, let’s only focus on access specifiers. In a class, it is generally recommended to keep your fields private, and have public methods to alter them if any. Basically, the idea is to have control over the alteration happening on the field in question. We have the following access specifiers with the following scopes - public (accessible from anywhere), private (accessible within the class), protected (accessible by classes and subclasses within the same package), and default, which is the access specifier used when no access specifier is mentioned (scope is limited to the package in which it exists).
So, following from this, we can kind of make out why public fields might be considered “bad” design. Basically, with a reference to the class, it is possible to change any public variable in that class from anywhere. Now there are some very obvious situations where we wouldn’t want the user changing data at whim, or at least where we would want to verify/check the data given by the user. With public fields, there really is no way to do this. So to get past this, private/protected fields are used. They allow the user to interact with our fields, but at the same time as a programmer, they give us a level of security, to monitor the changes being made to our fields. This security can be used to make sure you don’t ever get input that can potentially crash your program or other uses.
In java, to find the length of an array, we use arr.length and not arr.length(). If you notice, this is a field access. Since, length does not have parentheses after it, this tells me that it’s not a method, so it has to be a field. Well, how does Java keep track of the length of the array safely? How does it prevent the user from just randomly setting the array length to some number out of the blue? And also why does java use field access as opposed to method access even though it might be considered “bad” programming?
int[] arr = new int[10] //array with 10 spaces. System.out.println(arr.length) //prints out 10 //What’s stopping me from saying arr.length = 20 //or arr.length = 5
Now we know that the last two lines would raise an error, but even though java is using fields, why can’t we do this? The answer lies with the final keyword. When a variable or field is declared with the final keyword, it’s value cannot be changed after initialization. If you have a reference variable, you can’t change the object it refers to, but you can modify that object. Basically, once a reference to a certain object is made using final, that reference is here to stay. Final can also be used on methods and classes along with variables.
//final fields final int a = 9; a=10; //raises an error //final reference variables final Employee emp = new Employee(); emp.setId(“123”); emp.setId(“999”); // this is allowed as we’re only modifying the object while keeping the reference intact. emp = null; //here, we’re trying to change the entire reference, which gives an error.