Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Init Block Problem  RSS feed

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can We declare static Init Block within a class' method .If no,then give a proper reason for that.
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can We declare static Init Block within a class' method



Write a small code and see will it allowed or not.
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO

because static init blocks are meant for initialization of static variables of the class at class loading time
whereas methods are invoked programatically.
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not . I want to know why?
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





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


 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay ,the problem is resolved .Thanks for the solution.
Regards.
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can you define class inside methods ?



What about local inner class?
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can define a class with in a method if that's nested.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya we can define method local inner classes.....i missed it...!!!
You are right !!!




 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrapper classes are immutable.

Can someone post an example here to show this immutability of Wrapper Objects.
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay as you wish.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks (Link to new thread)
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!