Question

I just got an assignment back, which was marked down due to the following method definition:

static String[] getDayData (int day) {

I was told:

Your functions should always have the definer public/private/protected in them.

Despite the fact that I disagree with the function part in Java, It clearly states here that:

The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a body between braces, {}.

So who has any thoughts on this? Should I always include a modifier in the method's definition or not?

Was it helpful?

Solution

That statement, taken at face value, is definitely wrong. If what you wanted to declare is a package-private method, then this is the only way to do it. That declaration cannot possibly be added an access modifier without having its meaning changed.

What exactly your teacher has in mind with his marking criteria is a separate issue, but from my own teaching experience I cannot agree with that comment. Teaching Java 101 absolutely does include all method access levels.

On a side note, I can also not agree with his arbitrary use of "function" instead of the official term "method". This is just bad teaching in my book, and whether the underlying cause is ignorance of Java or just misguided didactics is not to be discerned from the information we have. And... did he really use the term "definer"? This term doesn't even exist—whether in Java or elsewhere.

OTHER TIPS

When programming in a team, not everyone will always be familiar with the language you are employing. The Java language specification does not require an access modifier for class members, but if you state it explicitly, it will be clear to everyone what you intend to do. The only exception of course, is the case Marko Topolnik mentioned, where you need a package private method. Although, I believe there are few common design patterns that make use of those. I can't even think of a logical situation where you'd need them.

There are four visibility levels for Java fields and methods, each "wider" than the last:

  1. private - visible only within this class
  2. "default" (i.e. no modifier) - visible to this class and other classes in the same package
  3. protected - visible to this class, other classes in the same package, and also subclasses of this class that are in other packages
  4. public - visible everywhere

so it is valid to have a method with no modifier. Some coding styles I've seen make package visibility more explicit with a comment like

/* package-private */ static int foo() { ... }

While this might be considered a style question and you could be graded on that basis, methods and classes using default visibility can be valuable. The default visibility in Java is package level visibility see this part of the Java tutorial. I tend to use it when writing libraries. There are functions, and some fields, which I want to access from within other classes in my library, but are intended solely for internal use. When writing in other languages, such as php, I find myself missing this level of visibility and instead resorting to comments with functions declared public saying they are "Internal Only". With this default level it's important to note that subpackages are considered different packages and won't be able to see functions with this visibility level.

It's unlikely to get you points back as you probably intended it for use outside of the package.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top