File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Whats the use of static block here in the code given ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Whats the use of static block here in the code given ?" Watch "Whats the use of static block here in the code given ?" New topic
Author

Whats the use of static block here in the code given ?

maganti suryanarayana
Ranch Hand

Joined: Mar 30, 2010
Posts: 53

Whats the use of static block here in the code given ?



Whats the meaning of public static final set OBJECT_SET ?
What is the meaning of unmodifiableSet method ?


Thanks & Regards
Surya

surya
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2431
    
  28

Read this for an explanation of class variables and this for an explanation of static initializers
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The intent is to have a Set (a collection of unique Objects) which can be read from, but can not be modified. That static block creates a HashSet - a Set which is modifiable - fills it with values, then assigns the OBJECT_SET reference to an unmodifiable view. That means the set is filled just once, when the class is loaded, but afterwards can not change. This makes use of the Set safe to use without fear that some code may change the Set when it is not supposed to be changed - it can't have Objects removed or added to it.


Steve
maganti suryanarayana
Ranch Hand

Joined: Mar 30, 2010
Posts: 53

Steve Luke wrote:The intent is to have a Set (a collection of unique Objects) which can be read from, but can not be modified. That static block creates a HashSet - a Set which is modifiable - fills it with values, then assigns the OBJECT_SET reference to an unmodifiable view. That means the set is filled just once, when the class is loaded, but afterwards can not change. This makes use of the Set safe to use without fear that some code may change the Set when it is not supposed to be changed - it can't have Objects removed or added to it.


Thanks Steve for the response. I do have one question here. you said you cant have objects removed or added to it and its true as its in static modifier. If its not in static modifier then what differences it make with a static block here.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

1. OBJECT_SET cant be modified in it's size . but still it is mutable- contents can be modified
2. you can replace static initializer by static method.
3. if OBJECT_SET is not static, then you cant access that reference in static block.
maganti suryanarayana
Ranch Hand

Joined: Mar 30, 2010
Posts: 53

Seetharaman Venkatasamy wrote:1. OBJECT_SET cant be modified in it's size . but still it is mutable- contents can be modified
2. you can replace static initializer by static method.
3. if OBJECT_SET is not static, then you cant access that reference in static block.


Thanks for the information seetharam. I have understood the importance of unmodifiable which says that the contents of the set cannot be modified. public static final for the fixing of the size of the set.


Thanks once again seetharam
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

maganti suryanarayana wrote:Thanks Steve for the response. I do have one question here. you said you cant have objects removed or added to it and its true as its in static modifier.

The fact that it can't be added to or removed from comes from the fact that it is a reference returned from Collections.unmodifiableSet(objectSet);, not that it is static. The variable can't be changed (assigned a different Set to reference) because the variable is marked final, not because it is static.

If its not in static modifier then what differences it make with a static block here.


You need something to fill the Set, and that must be done in a block. You could fill the Set in a method, but there are two problems with that:
1) Relying on someone or something to call a static method to initialize the values could mean the Set could be accessed when it is not initialized with values yet. This would make using any method that requires the Set unsafe. By putting the initialization in a static block you ensure the initialization happens when the code is loaded and before any method could access it. Making it always safe to use.

2) If you use the a method to initialize the Set then the Set could not be declared final, because there is no way to ensure that the Set was initialized before the class was finished loading. If the variable isn't final its value could be re-set to a different Set at some point after it is first created. There would also be no control over that Set - it could be assigned a modifiable Set instead of an unmodifiable Set. So making the variable non-final would mean any method using the Set would not be safe. And making the variable final would mean it must be initialized either inline (which you can only do with simple assignment) or using a static initializer.

So for these two reasons the static initialization block is the only means to get what was required in a static context.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Seetharaman Venkatasamy wrote:1. OBJECT_SET cant be modified in it's size . but still it is mutable- contents can be modified

That is correct. The JPanel and ArrayList are particularly dangerous in this respect. The other Objects are all immutable so not a worry.

Seetharaman Venkatasamy wrote:2. you can replace static initializer by static method.

Actually not, see my above reply about the Set being final.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Steve Luke wrote:
Seetharaman Venkatasamy wrote:2. you can replace static initializer by static method.

Actually not, see my above reply about the Set being final.

Steve, you can do as in so, guaranteed visibility to threads.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Seetharaman Venkatasamy wrote:


How did I not think of that! I was thinking only of using . One of the reasons I don't like to call methods during initializers (or in intializer blocks [static or not], or in constructors) is that you get 'reference leak' where you can refer to variables before the class is finished loading (or for instance intializers/constructors, before the instance is done being initialized). So I tend to avoid them enough I rarely see them as a viable option.

In this case it is a viable option, as long as you don't refer to any other static value in the method.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Steve Luke wrote: as long as you don't refer to any other static value in the method.

as far i know, it wont harm irrespective of setInt/getInt untill they are static. static initializer can replaced by static methods. i may wrong, but just want to know that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Whats the use of static block here in the code given ?