Explain role of constructor in a java application?
Answer Posted / 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 |
Post New Answer View All Answers
What is the base class of all exception classes?
What is time complexity algorithm?
Explain the pointers in Java?
Are there structures in java?
What is the default value of local and global variables?
Can singleton class be serialized?
What do you mean by compiler?
What is method overriding in java ?
What is the primitive type short?
Explain about the performance aspects of core java?
What are different types of control structures?
What is float in java?
Is java ee a framework?
How will you reverse a link list without using recursion?
Why stringbuilder is not thread safe?