Site Network: Home | About

Following would not add any value to you if you are already working as a Java developer. Think once before scrolling down. So u've decided to go down. Ok, let us go. How do u initialize an instance variable? - studpid question, right? yes, it is. we do it in constructor also, we can directly initiaze while declaring. Same question, but it is regarding initialization of Class member. plz don't say I use the constructor for this also. Ofcourse u won't tell, bcoz Class variable doesn't belong to any of the instances.(contructor would be called while instantiating a Class) Hey, I can directly assign the Class variable using binary operators like =, += etc while it's declaration. Same is valid for ternary operator(?:) also. Yes, u can provide an initial value for a class or an instance member variable in its declaration like public class BedAndBreakfast { //initialize to 10 public static final int MAX_CAPACITY = 10; //initialize to false private boolean full = false; } But, don't u think it has some limitations. If u don't think plz try to think abt these questions.

  • How can I call a method or use a stmt(if-else, case etc) to assign the returned value.
  • How can JVM handle the exception that is raised by initialization expression or by method called .
I think below code would answer my questions. class Errors { static ResourceBundle errorStrings; static { try { errorStrings = ResourceBundle.getBundle("ErrorStrings"); } catch (MissingResourceException e) { //error recovery code here } } } The errorStrings resource bundle must be initialized in a static initialization block because the getBundle method can throw an exception if the bundle cannot be found. The code should perform error recovery. Also, errorStrings is a class member, so it should not be initialized in a constructor. OVER!!!. Thanks for bearing with me -- Karteeek

0 Comments:

Post a Comment