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
How do you sort arraylist in descending order?
Is break statement can be used as labels in java?
What is dynamic array in java?
What are the methods used to implement for the key object in the hash map?
Is there a way to increase the size of an array after its declaration?
Why do we use threads in java?
Is it safe to install java on my computer?
Difference between predicate, supplier and consumer ?
How can you make a class serializable in java?
What does the append?
What is a Hash Table? What are the advantages of using a hash table?
Explain the private protected method modifier?
Explain enumeration in java?
What lambda means?
Can you sort a list in java?