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 are the various access specifiers in java?
What is == mean?
Does java runtime require a license?
Explain 5 features introduced in jdk 1.7?
What is difference between wait and notify in java?
Does importing a package imports its sub-packages as well in java?
what are three ways in which a thread can enter the waiting state? : Java thread
why Java does not support multiple inheritances?
What are alternatives to java serialization?
What is meant by tab pans?
What is the abstract class?
When does a class need a virtual destructor?
What is charat ()?
What is string pool?
What is difference between array and vector?