-4

There are several modifiers used before declaring a java method such as public, static, synchronized etc.

I just want to know the maximum numbers of modifiers or all the combination of modifiers a java method can contain.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
MRHMisu
  • 31
  • 6
  • 6
    Read the [language spec](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.3). – Andy Turner Apr 14 '17 at 19:12
  • 1
    Possible duplicate of [Java modifiers syntax and format](http://stackoverflow.com/questions/7050233/java-modifiers-syntax-and-format) – Stephen P Apr 14 '17 at 19:21

2 Answers2

6

See the Java language spec, chapter 8.4:

MethodDeclaration:
  {MethodModifier} MethodHeader MethodBody

and:

 MethodModifier:
 (one of) 
 Annotation public protected private 
 abstract static final synchronized native strictfp

You can't mix:

  • the access modifiers (so you got one of those 3, or none for package protected)
  • abstract, static, final
  • abstract with (private, static, final, native, strictfp, synchronized)
  • and finally: native and strictfp

Taking all of that together (thanks to user Andreas for the excellent wording):

Using regex syntax, we get to:

 [ public | protected | private] static final synchronized [native | strictfp]

So, the maximum number is 5; and 6 different combinations of those 5 keywords.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Some of them are mutually exclusive, so just listing them all doesn't answer the question. – Andreas Apr 14 '17 at 19:30
  • Incomplete, since spec says "can't mix `abstract` with any of `private`, `static`, `final`, `native`, `strictfp`, or `synchronized`" and "can't mix `native`and `strictfp`". – Andreas Apr 14 '17 at 19:35
  • So that leaves `[public | protected | private] static final synchronized [native | strictfp]`, i.e. answer is 5, and there are 6 combinations of that length. – Andreas Apr 14 '17 at 19:36
  • Thanks for all that input; reworded again! Showed my gratitude elsewhere! – GhostCat Apr 14 '17 at 19:39
  • That is not regex syntax. Still, +1. – Andreas Apr 14 '17 at 20:12
  • Hey, Ghost, someone downvoted [my answer](http://stackoverflow.com/a/43418203/5221149), and I'm wondering why. Can you see anything wrong with it? – Andreas Apr 14 '17 at 20:59
  • Nothing that would explain a downvote. To the contrary. – GhostCat Apr 15 '17 at 04:08
1

According to the Java spec, §8.4.3. Method Modifiers, the total list of modifies are (not counting annotations):

public protected private
abstract static final synchronized native strictfp

public, protected, and private are mutually exclusive, though that section doesn't say that.

The spec also says:

It is a compile-time error if a method declaration that contains the keyword abstract also contains any one of the keywords private, static, final, native, strictfp, or synchronized.

So if you include abstract that only leaves public | protected, so max of 2.

The next rule in the spec says:

It is a compile-time error if a method declaration that contains the keyword native also contains strictfp.

So, this means that without abstract, you can mix as follows:

public | protected | private
static
final
synchronized
native | strictfp

Max length of 5, and there are 3 * 2 = 6 combinations with that length.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • In a degenerate way, lack of any of the modifiers `public`, `protected`, or `private` on a member is a modifier, the "default" or "package-private" phantom access modifier. – Lew Bloch Apr 14 '17 at 23:01
  • @LewBloch I disagree. The word "modifier" means that it modifies something. In this case, a method is package-private, unless a modifier changes it. As such, package-private is not a modifier, it's the default value/state. Note that there are no modifier keywords for specifying the default state, e.g. `virtual` for opposite of `final`. All the keywords truly *are* modifiers. – Andreas Apr 15 '17 at 00:15
  • Note: "In a degenerate way". You're correct, of course, but ironically, in practice "default" access is not the default. People have to make a conscious choice to use default access, if they're actually programming. Most seem to understand `public`, `protected`, and `private` well enough. Package-private has its engineering purposes​ and is on an equal footing with the others, except when it comes to having its own keyword. – Lew Bloch Apr 15 '17 at 03:33