Java code understanding help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Java code understanding help

  1. #1
    Join Date
    Aug 2013
    Posts
    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;
    }
    }

  2. #2
    Join Date
    Jan 2012
    Posts
    17

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center