aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Init Block Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Init Block Problem" Watch "Static Init Block Problem" New topic
Author

Static Init Block Problem

Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Can We declare static Init Block within a class' method .If no,then give a proper reason for that.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Can We declare static Init Block within a class' method


Write a small code and see will it allowed or not.


Life is easy because we write the source code.....
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
NO

because static init blocks are meant for initialization of static variables of the class at class loading time
whereas methods are invoked programatically.

SCJP 6.0 96%

(Connecting the Dots ....)
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
It does not . I want to know why?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Use static block when there is s a requirement to execute block of code when the class is loaded but before instantiating the class. If you write static block of code inside method then you must have instance of class to execute that method and the whole purpose of static initialization block is fail. Hence it will not allowed.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316





Oooops .....We have to follow the rules mentioned JAVA language designers.


Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
okay ,the problem is resolved .Thanks for the solution.
Regards.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Can you define class inside methods ?


What about local inner class?
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
we can define a class with in a method if that's nested.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Ya we can define method local inner classes.....i missed it...!!!
You are right !!!




Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Wrapper classes are immutable.

Can someone post an example here to show this immutability of Wrapper Objects.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Harshit Sethi wrote:Wrapper classes are immutable.

Can someone post an example here to show this immutability of Wrapper Objects.

I think it would be better to start a new thread


[My Blog]
All roads lead to JavaRanch
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Okay as you wish.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Thanks (Link to new thread)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Init Block Problem