0

I have just started learning Java a few weeks ago. I'm trying to learn the key word final in java language. I wrote a final string statement in the public static void main method. However, the IDE is displaying the error: illegal start expression. However, the IDE did not provide any additional information. Because I'm relatively new to Java, I am unable to understand why this is happening. Could someone please explain the reason to me?

public static void main(String[] args) {
    private final String s1 = "hello world";
}
Thor
  • 9,638
  • 15
  • 62
  • 137
  • @HovercraftFullOfEels Thank you so much for helping out? May I ask why we can't put private keyword within a method? – Thor Mar 02 '16 at 22:59
  • 1
    Ask yourself, what would it mean? The variable is local to he method regardless and cannot be seen outside of the method scope, so adding a `private` modifier would be senseless. – Hovercraft Full Of Eels Mar 02 '16 at 22:59
  • that make sense. Thanks again for your help! – Thor Mar 02 '16 at 23:00

1 Answers1

1

There is no such thing as a private field within a method, and this only makes sense within class scope, not method scope. Get rid of the private modifier.

To get a handle on this, ask yourself, what would it mean? The variable s1 is local to he method regardless and so cannot be seen outside of the method scope, so adding a private modifier would be senseless as it wouldn't change anything.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373