Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining methods in Java [closed]

Tags:

java

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?

like image 854
Conor Taylor Avatar asked Dec 07 '22 10:12

Conor Taylor


1 Answers

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.

like image 129
Marko Topolnik Avatar answered Dec 22 '22 01:12

Marko Topolnik