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

What are the application of stack?

493


What is assembly language?

546


Which sorting algorithm is in place?

533


What is a parameter used for?

507


Which oo concept is achieved by using overloading and overriding?

529






Define jre i.e. Java runtime environment?

568


What is a char in java?

527


What is covariant return type?

633


What is temp in java?

542


Why java is not 100% object-oriented?

822


Do I need java on my pc?

503


What is singleton class example?

582


How do you add an element to a set in java?

491


Explain constructors and types of constructors in java.

637


Is call by reference possible in java?

527