Java: Static Variables and Methods

My first Java related blog post.

Static Variables
Static variables can be accessed from all instances of an object and without an instance of the object existing. When the static keyword is used on a variable, its value is then the same for all instances of that object. This is a way of creating global variables in java.
Static Methods
Static methods belong to the class, not to the individual instances of a class. These methods can be called before an instance of the class is created. This method cannot see any instance variables or call any instance methods because we would have to know which instance it is referencing, which may not exist.

My Personal Learning Experience Related to This Topic

I had recently been working on a programming project called DateCalculator to demonstrate competency in the fundamentals of programming for a college class. The program was to take a user entered day in the format mm/dd/yyyy, and return a date x days in the future. The entered date was required to be between the year 2000 and 2025, and the number of days to be added amounting to 1 to 60 days. We also had to take leap year logic into account. The requirements also required that we use an array. You can find the DateCalculator program on my Github.

I had worked on the project over a weekend. I decided that to use the required array to store each incremental day for the number of days that the user wants to add to the entered date.

Ex)
User enters 05/12/2000 and wants to add 4 days to it: so the array created would then hold: 05/13/2000, 05/14/2000, 05/15/2000, 05/16/2000.

I decided to use a Date object to hold the date’s information. from this class you could access an entered month and year to derive the maximum number of days in that month, such as 28 days in February of 2001, and 29 days in February of 2000. The program would then return the last element of the array as the final result.

I finally finished the project, or so I thought and was ready to test it. I was running into a problem where each element of the array had the same value, even though when I walked through my code and had come to the conclusion that my logic was correct.

Ex)
User entered 05/12/2000 and wants to add 4 days to it: so the array created actually held: 05/16/2000, 05/16/2000, 05/16/2000, 05/16/2000. Instead of the intended dates.

What I had overlooked is that since I had been calling the functions of the DateCalculator Class from the main (static) method, I had to call other static methods from there, which in turn had to have static variables. I had made the Date object’s “instance” variables (month, day, year) static. This meant that each date had shared values for their month, day, and year, so when I created a new date and set it’s values, I was changing the values of all the Date Instances. I had corrected this by moving the code now contained in the DateCalculator class, which was previously in the main method, and creating an instance of the Date calculator in the main method instead.

Published by Enrique Ramirez

Aspiring Software Developer | CompTIA A+ Certified

Leave a comment