I am confused, why would this lines of code not work? Thank you.
public class HelloProgram{
public static void main(String args[]){
private static final double x = 3;
}
}
Printable View
I am confused, why would this lines of code not work? Thank you.
public class HelloProgram{
public static void main(String args[]){
private static final double x = 3;
}
}
Hi lockmantican,
What you are trying to do here doesn't make sense. When you declare a variable in a method it is a local variable and is only accessible to the method in which you declare the variable. Therefore there is never a need to use public/private/static etc. within methods and therefore java won't allow such statements. I assume the error you get is "illegal start of expression"?Code:private static final double x = 3;
The only times you will need to do something like this is if you are declaring the variables outside all methods in the class as a global variable. You can however use the keyword "final" this is perfectly acceptable however I would suggest you understand why and how you would use it before you begin doing so.
I suggest you read up about local/global variables to get a better understanding as my explanation may not be the best.
Hope this helps some bit.