You are on page 1of 1

Constructors Constructors can be considered as special methods with the same name of the clas s used to initialize an object.

Whenever an object is created, a constructor is executed. A default constructor that has no arguments is provided automatically for all classes if there are no user defined constructors. If the developer prov ides at least one constructor, the compiler's default constructor is no longer a dded. It is always good have an explicit, default, no-argument constructor. Blocks within methods When a block without any name appear inside a method, say main method, it is con sidered as a combined statement of the parent method and will be executed when t he containing method is executed. Consider the example an inner block within the main method: public static void main(String[] args) //outer block { int var1=0; int var2; //inner block without name { int var3=0; System.out.println("var1="+var1); System.out.println("var3="+var3); } System.out.println("var1="+var1); /* Below line won't compile as var2 is not initialized. */ //System.out.println("var2="+var2); /* Below line won't compile as var3 is not visible outside the block in which it was defined. */ //System.out.println("var3="+var3); }

You might also like