Results 1 to 7 of 7

Thread: Static Blocks

  1. #1
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Static Blocks

    My teacher, in class today, went over static blocks. After the lecture, I wanted to learn more about it, so I read this article, and still have some questions. Is there really any difference between these two snippets of code:
    Java Code:
    class deckHand {
    	private static String[] _suitStr;
    	
    	static{
    		_suitStr[1] = "One";
    		_suitStr[2] = "Two";
    		_suitStr[3] = "Three";
    	}
    and

    Java Code:
    class deckHand {
    	private static String[] _suitStr = {"One", "Two", "Three"};
    They both seem to work the same; am I wrong?..


    Also, how come I cannot do this:
    Java Code:
    class deckHand {
    	
    	static{
    		private static String[] _suitStr = {"One", "Two", "Three"};
    	}
    It gives me an error saying:
    Illegal modifier for the variable _suitStr; only final is permitted
    After I remove private and static, it seems to work perfect; but with them, it pops up the error... Am I missing something here?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Static Blocks

    Quote Originally Posted by penguinCoder View Post
    Is there really any difference between these two snippets of code:
    Java Code:
    class deckHand {
    	private static String[] _suitStr;
    	
    	static{
    		_suitStr[1] = "One";
    		_suitStr[2] = "Two";
    		_suitStr[3] = "Three";
    	}
    and

    Java Code:
    class deckHand {
    	private static String[] _suitStr = {"One", "Two", "Three"};
    There's no difference that I can see. The Java Language Specification, 12.4.2. Detailed Initialization Procedure has this as step (9) "Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block." So basically the code just gets executed from the top with the same result either way.

    But suppose that instead of assigning the array contents as string literals you were to populate the array with, say, database calls or some other operation that could potentially throw an exception. The static initialiser version at least allows you to use try/catch and do something with the exception - in fact it may be required that you catch the exception.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Static Blocks

    Quote Originally Posted by penguinCoder View Post
    Also, how come I cannot do this:
    Java Code:
    class deckHand {
    	
    	static{
    		private static String[] _suitStr = {"One", "Two", "Three"};
    	}
    It gives me an error saying:


    After I remove private and static, it seems to work perfect; but with them, it pops up the error... Am I missing something here?
    I think you may be missing the fact that, if you remove "private static", the resulting code "works" in the sense that it shuts the compiler up, but it doesn't do anything useful. More precisely _suitStr is a local variable. Like every variable, it is only visible in the block within which it is declared. _suitStr is not visible anywhere else in the class (or elsewhere) and, so, cannot function as an honest-to-goodness static variable.

    Basically declaring _suitStr as static and/or private makes no more sense within a static block than it does within a constructor or method block.

  4. #4
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Static Blocks

    Quote Originally Posted by penguinCoder View Post
    Also, how come I cannot do this:
    Java Code:
    class deckHand {
    	
    	static{
    		private static String[] _suitStr = {"One", "Two", "Three"};
    	}
    It gives me an error saying:


    After I remove private and static, it seems to work perfect; but with them, it pops up the error... Am I missing something here?
    Well static means that it belongs to the class it self and stays the same for all instances of that class (I can explain this further if you need). That being said that code block can have static applied to it but if you think about it it doesn't make sense for the String[] to specify static because its already static because its a part of the static block. It couldn't really be "more" static then it already is. If I'm correct I believe a static block runs only once right when the class comes into existence and it runs earlier then methods so you wouldn't be able to call the String[] outside of the class anyways because the code block will be done before you could do so. So in a way its already private and can't be accessed by outside classes. Pretty sure that's why but anyone who knows something I missed or miss understand let me know. If you need me to explain further let me know.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Static Blocks

    Things declared inside code blocks (whether static blocks, methods or constructors) cannot have access modifiers (ie private, protected, public) or be class members (ie static) or transient/volatile. They are simply local variables.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Static Blocks

    Thanks for the replies, this has really helped clear the issue up for me. I have a much firmer grasp on the issue now, I appreciate it.

  7. #7
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Static Blocks

    Quote Originally Posted by penguinCoder View Post
    Thanks for the replies, this has really helped clear the issue up for me. I have a much firmer grasp on the issue now, I appreciate it.
    no problem

Similar Threads

  1. use of code blocks in java
    By b4viral in forum New To Java
    Replies: 5
    Last Post: 09-06-2012, 09:02 AM
  2. Java Blocks??
    By abimaran in forum New To Java
    Replies: 24
    Last Post: 11-07-2009, 03:22 PM
  3. alternative to static initialization blocks
    By paola in forum New To Java
    Replies: 1
    Last Post: 12-06-2008, 05:51 PM
  4. Blocks Language 0.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 09-22-2007, 11:21 PM
  5. Task Blocks 0.5
    By johnt in forum Java Software
    Replies: 0
    Last Post: 08-08-2007, 08:43 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
  •