Results 1 to 5 of 5
  1. #1
    hara is offline Member
    Join Date
    Apr 2016
    Posts
    3
    Rep Power
    0

    Smile Why re-initializing the static variable in separate line is invalid?

    Please help me understand this why this is not compiled??

    Why re-initializing the static variable in separate line is invalid??

    public class Test {

    static int a;
    a =10; //why its invalid

    public static void main(String[] arg){
    System.out.print(a);
    }
    }


    compilation error: identifier expected why initializing a=10; is not valid whereas static int a=10; is working fine

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why re-initializing the static variable in separate line is invalid?

    Because it is illegal. As to why, I would have to guess that the language designers figured the extra logic to include in the syntax parser was not worth the effort.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    hara is offline Member
    Join Date
    Apr 2016
    Posts
    3
    Rep Power
    0

    Default Re: Why re-initializing the static variable in separate line is invalid?

    is there any source that can explain this particular invalidity

    i am confused

    is there any documentation around this one presents??

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Why re-initializing the static variable in separate line is invalid?

    If arbitrary statements (such as 'a= 10;') where allowed outside the body of a method, it would be easy to stop initialization in a class (at the static level) or an object (non-static level) by simply putting in 'for(;;);'. although this still possible in initialization blocks such as { ... } or static { ... } you'd get what you deserve ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why re-initializing the static variable in separate line is invalid?

    It is unlikely. A lot of what is or is not allowed is hashed around in committees by the language designers. Very seldom do folks know why something is allowed or not. Detailed information on what is or is not allowed is explained in the Java Language Specification. You can get it here. The Java Language Specification . It is not an easy and with perhaps rare exceptions (I can't think of one) does not explain the thought process in how specific constructs of the language were chosen.

    Edit:

    Upon after thinking about this, you might as well have asked,

    Why can I do this:

    Java Code:
     
    
    class foo {
        int [] vals = {1,2,3,4,5,6,7,8,9,10};
        public static void main...
    }
    but not this?

    Java Code:
    class foo {
       int [] vals = new int[10];
       for (int i = 0; i < vals.length; i++) {
    	  vals[i] = i+1;
       }
       public static void main....
    }
    Again, because it is simply not supported.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 02:00 PM
  2. Replies: 2
    Last Post: 04-07-2012, 12:48 PM
  3. Replies: 4
    Last Post: 07-19-2011, 02:30 PM
  4. Initializing a variable from another method
    By erikjd21 in forum New To Java
    Replies: 4
    Last Post: 04-17-2011, 09:55 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •