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.
Q31. 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 :
Q32. 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 :
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 :
public static void main(String[] args){
System.out.println("Main Method");
}
}
Static Block
Main Method
Static Block
Instance Initialization Block
Main Method
Static Block
Constructor
Main Method
Static Block
Instance Initialization Block
Constructor
Main Method
Q35. Why do we use a copy constructor ?
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. Yes, When either we don't want an object to be created ( class having all static elements and hence not required ) or object to be created using a static method or static block.
One such example could be a situation when we would like app to load objects at start up and we would like to restrict new object creation by any request.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Java provides a default constructor ( no argument ) if no constructor is provided by the programmer. Java makes a check during pre compilation if there is any constructor provided by the programmer. If any constructor is provided, Java understands that programmer has provided all the mechanism for initialization and construction of the object and may not even intend to create objects by calling a no argument constructor. For example - One may like to have objects created with self defined state otherwise restrict its creation.
We make the constructor private if we want to make a class singleton as we would like to restrict creation of new objects using new operator and default constructor. Similar could be the situation wherein we would like to construct objects in a particular manner only.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Constructor's objective is to initialize member elements. If we don't deviate from that and don't have methods with logic in them, I don't see an issue with constructor's calling other methods assuming the methods are doing nothing but initialization. There are situations wherein we classify the member elements based on their type and hence having different initialization methods for different element types will give a better abstracted way to initialize them.
Help us improve. Please let us know the company, where you were asked this question :
Ans. I would avoid that. If we need to initialize member elements differently on the basis of some condition, I would prefer having overloaded constructors. I don't see a need to have a loop for initializing member elements unless the count of elements is huge and they all need to be initialized with common value.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Constructors are used to initialize the state of an object. If there are no constructor , objects won't have any initialized state and hence it's elements may contain garbage values. If the memory were used as is, the behavior of the object would also be unpredictable.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Public constructor is simple and easy as it's the default way of object creation. So there are no additional coding overheads as compiler provides the default constructor if none is provided by coder.
With static final methods, it facilitates loose coupling by segregating the responsibility of object creation to a separate method. Validation can be done on the constructor arguments before calling it. Moreover if any adaption on the arguments is required that can achieved easily with factory method.On the flip side, there is coding overhead and additional method call.
Ans. All methods are not expected to return something but Yes, all methods are expected to have a return type. If a method returns nothing, it can be declared with the return type void.
Constructors are not expected to have any return types , not even void.
Help us improve. Please let us know the company, where you were asked this question :
Ans. BuggyBread method without any return type is the constructor which get's called upon object creation whereas BuggyBread method with return type of void is just another method that needs to be called explicitly for it's invocation.
Help us improve. Please let us know the company, where you were asked this question :
Ans. No, first method is a constructor whereas the second method is just a normal method. There is no way a constructor can be called explicitly and hence all explicit calls to BuggyBread() would result in compilation error.
Help us improve. Please let us know the company, where you were asked this question :