How java stores array length safely.

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.





To view or add a comment, sign in

More articles by Om Kudalkar

  • Deploying a Server using IaC

    In my previous article, I gave a quick run-down of what IaC is, why it's lucrative, and why adopting it is beneficial…

  • An Introduction to Infrastructure as Code

    "IaC" or Infrastructure as Code, is simply the ability to provision, manage and support your computing infrastructure…

    3 Comments
  • How to add dependencies to a Lambda Function in AWS

    The Lambda Python runtimes includes the AWS SDK for Python (Boto3) and its dependencies, but this does not include a…

    1 Comment
  • Unpaid Internships - yea or nay? Nay.

    Today, I came across this post by Ishan Arora and I was initially reluctant to say anything especially on a…

  • How Good Design Implementation Prevents Errors

    Over this semester I’m taking Object Oriented Programming and Design and till now, I’ve learned a lot. I now have a…

  • Useful Intraday Trading Indicators

    Top Indicators for day trading - 5 and 10 EMA (Exponential Moving Average) Bollinger Band (BB) RSI Volume A. 5 and 10…

    2 Comments
  • How you can gain control of your free time.

    This was a particularly interesting talk and a very powerful one. It opened my eyes to some of the mistakes I was doing…

    2 Comments
  • Yes, Binary Search is O(log(n)), but how?

    We know that the Binary Search algorithm is of O(log(n)) complexity, but how do we arrive at this conclusion? Is there…

    2 Comments
  • HOW YOU CAN SELL ANYTHING

    Derek Thompson shares the 4-letter code to sell anything. Quite recently, I watched Derek Thompson's Ted talk which…

Explore content categories