0

I'm a beginner in Java and I'm having problems to declare constants as public, after searching a while in some books, I created this very simple program:

package course1;

public class Circle 
{
    public static void main(String[] args) 
    {       
        public static final double QUARTER_VALUE = 0.25;
        System.out.println(QUARTER_VALUE);
    }
}

I got the following error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Illegal modifier for parameter QUARTER_VALUE; only final is permitted

    at course1.Circle.main(Circle.java:7)

The problem is in this line:

public static final double QUARTER_VALUE = 0.25;

I was given the following message when I hover this line:

illegal modifier for parameter QUARTER_VALUE; only final is permitted  

I don't understand why, following my books this should work.

kutschkem
  • 7,826
  • 3
  • 21
  • 56
user804406
  • 73
  • 5

1 Answers1

1

The scope of a method variable is only that method, so you do not need to specify the scope explicitly. A variable declared directly within a method is a local variable. A local variable cannot be declared as public. In fact, the only modifier that you can use on a local variable declaration is final

The access modifiers in case of variables are only applicable to class members and not method (local) variables.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Tahir Hussain Mir
  • 2,506
  • 2
  • 19
  • 26