More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. Copy Constructor is used for creating a duplicate of another object. Duplicate object will have the same state of the copied object but will have independent values in different memory locations. Copy Constructor can be useful to create duplicates of immutable objects as the Original cannot be tampered. Moreover It can be useful if base copies are required for individual requests in threading.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Constructor has the same name as class name whereas instance initialization block just have a body without any name or visibility type.
instance initialization blocks are useful if we want to have some code run regardless of which constructor is used or if we want to do some instance initialization for anonymous classes.
Help us improve. Please let us know the company, where you were asked this question :
We cannot use return type void with overloaded methods
Q34. Why doesn't Java even allow void return types for Constructors ?
Ans. Because having a void return type with the Constructor name will make it as a method name. As methods with the Class names are perfectly legit in Java, It needs some way to recognize the constructor and having none return type helps identify that.
Help us improve. Please let us know the company, where you were asked this question :
We cannot use return type void with overloaded methods
Q35. Does Constructor returns the reference of the newly created object ?
Ans. No, New operator actually returns the reference of newly created object. Constructor's purpose is to initialize the default state of the object by setting the initial values of the elements and hence returns nothing.
Help us improve. Please let us know the company, where you were asked this question :
Q36. What will happen if we have our own constructor but only initialize some of the member elements ?
Ans. Its always advisable to initialize all the member elements in case we don't use the compiler's default constructor as leftover member elements will have garbage values and may reflect and unstable state for the object.
Help us improve. Please let us know the company, where you were asked this question :
Q37. Is it advisable to set the member variables through constructors instead of setting them through setters ?
Ans. Yes , If the values to be set are known at the time of initialization and doesn't involve polymorphic behavior.If it's using Dependency Injection , then Constructor injection must be available. If it suffice the above conditions, then definitely its advisable to have them set through constructor as they eagerly load the values into the memory and save it fro multiple values assignment ( one through default constructor and then through assignment )
Help us improve. Please let us know the company, where you were asked this question :
Ans. New operator in Java creates objects. Constructor is the later step in object creation. Constructor's job is to initialize the members after the object has reserved memory for itself.
Help us improve. Please let us know the company, where you were asked this question :
Ans. You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to override the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Though It's often confused with each other, Object Creation ( Instantiation ) and Initialization ( Construction ) are different things in Java. Construction follows object creation.
Object Creation is the process to create the object in memory and returning its handler. Java provides New keyword for object creation.
Initialization is the process of setting the initial / default values to the members. Constructor is used for this purpose. If we don't provide any constructor, Java provides one default implementation to set the default values according to the member data types.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1.this is a reference to the current object in which this keyword is used whereas super is a reference used to access members specific to the parent Class.
2.this is primarily used for accessing member variables if local variables have same name, for constructor chaining and for passing itself to some method whereas super is primarily used to initialize base class members within derived class constructor.
Help us improve. Please let us know the company, where you were asked this question :
We cannot have both super() and this() in a constructor
Q53. Difference between Class#getInstance() and new operator ?
Ans. Class.getInstance doesn't call the constructor whereas if we create an object using new operator , we need to have a matching constructor or copiler should provide a default constructor.
Help us improve. Please let us know the company, where you were asked this question :
Ans. No. Every Class only needs to have one constructor - With parameters or without parameters. Compiler provides a default non parameterized constructor if no constructors is defined.
Help us improve. Please let us know the company, where you were asked this question :
Ans. A child object constructor always first needs to construct its parent. In Java it is done via an implicit call to the no-args constructor as the first statement
Help us improve. Please let us know the company, where you were asked this question :
Ans. At the beginning of an object's life, the Java virtual machine (JVM) allocates memory on the heap to accommodate the object's instance variables. When that memory is first allocated, however, the data it contains is unpredictable. If the memory were used as is, the behavior of the object would also be unpredictable. To guard against such a scenario, Java makes certain that memory is initialized, at least to predictable default values before it is used by any code.
Help us improve. Please let us know the company, where you were asked this question :