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


Please Help Members By Posting Answers For Below Questions

Explain methods specific to list interface?

560


What is the file type?

561


What is an i/o filter?

571


how we can make a read-only class in java?

546


What does split function do in java?

540






What is the tradeoff between using an unordered array versus an ordered array?

666


why are there separate wait and sleep methods? : Java thread

583


What is a variable in java?

540


What are peerless components in java programming?

587


What exceptions occur during serialization?

612


What is e in java?

545


What is the public method modifier?

549


Can we convert stringbuffer to string?

568


What is a class variable?

573


What are the topics in advance java?

525