Explain role of constructor in a java application?
Answers were Sorted based on User's Feedback
Answer / sonal
Without a constructor u cannot make an object or instance
of a class.
Every class must have a constructor. If no constructor is
provided the complier will provide default constructor at
run time.
| Is This Answer Correct ? | 33 Yes | 2 No |
Answer / sindhura
A constructor in java has the same name as that of the
class. Whenever the statement such as classname objectname
is encountered, it calls the constructor to initialise the
values for the object. It doesnot return anything and as
soon as the object is created constructor would be called.
ex. : if there is a class called A
A a = new A();
here A a statement would call the constructor immediately
to initialise the values for a object.
| Is This Answer Correct ? | 10 Yes | 1 No |
Answer / mr. ashwani hundwani
A constructor is a member function of a class,which gets
automatically called when a class is instantiated(i.e when
object is created),compiler automatically provides default
constructor(with no arguments).
It has same name as that of class
***Constructor is best utilized for initializing the data
members.*****
rather than writing methods to provide the values to data
members we can pass them through consturctor at the time of
instantiation.EX-
import java.util.*;
class shape
{
int length;
public shape(int len)//constructor should be public
{
length=len;
}
}
class abc
{
public static void main(String a[])
{
shape obj=new shape(5);/*passing a integer value to
constructor of class shape*/
}
}
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / harini
The main purpose of construtor is to initialize instance
variables.
| Is This Answer Correct ? | 4 Yes | 1 No |
Answer / harsh
Constructor is "object creator".
its role is to create objects thats it.
| Is This Answer Correct ? | 3 Yes | 0 No |
What's the purpose of using break in each case of switch statement?
Is it possible to do method overloading and overriding at a time
What is a literal coding?
Why do we create threads in java?
What is the exact difference in between Unicast and Multicast object ?
Name and explain the types of ways which are used to pass arguments in any function in java.
What is a java predicate?
What are examples of modifiers?
What is Generic in java? Where can we write Generic ( class or method or objects or etc...)? with simple example? Thanks, Bose.
2 Answers Infosys, Tech Mahindra,
why we are calling j2se why not j1se or j3se or j4se..etc is there any reason.. can u please explain it... i shocked in my interview when they asked this question please explain ans
Differentiate between stringbuffer and string?
What is meant by Encapsulation? Can you write a class to explain encapsulation?