-
August 4th, 2013, 05:13 PM
#1
Java code understanding help
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;
}
}
-
August 6th, 2013, 09:08 AM
#2
Re: Java code understanding help
Hi lockmantican,
Code:
private static final double x = 3;
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"?
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|