Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Access Modifiers

Just read a rant about a developer that likes to use parentheses to make the order of operations explicit. He does not want a reader of his code to need to know the operator precedence rules. As an extension, he also explicitly declares the access modifier for any java function that he writes.

This started a big discussion on what it means to not specify an access modifier for a Java function. In C++ it is easy. C++ class members without an access modifier are private. C++ structure members without an access modifier are public. However in Java, not specifying an access modifier means the function is package private.

Package private has no keyword. It is the access allowed when you do not specify a modifier. This causes the function to only be accessible by other members in the package. This is different than public, private, or protected. I am sure I learned this some time ago in my Java college class. However today it was solidified.

Once You Go Static...

I am on a 7 Day Quest to write a game in Java. That's a tall order since I am only half way through a college Java class. But I am learning a lot.

For example, I am trying to conserve time due to the quick deadline. Therefore I am putting all my code in one class. This class has a static main function to run the program. Guess what? That function can only call other static functions in this class. That's not too object oriented.

Well perhaps the optimistic way of looking at this is that I have an object. But it is the only one of its kind. There is only one main. So the class with that has main must be a singleton. I am keeping some data in static variables. And I am breaking up functionality into static methods. But it still feels a little clunky.