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 main differences between notify and notifyAll in Java?
What is the difference between path and classpath variables?
what is method reference in java 8?
Can a static class have a constructor?
Java is pass by value or pass by reference? Explain
Explain the difference between comparator and comparable in java?
What is the use of hashmap in java?
What is Session reduplication and how its done?
Which method returns the length of a string?
Can an anonymous class be declared as implementing an interface and extending a class in java programming?
Explain the difference between jdk, jre, and jvm?
What are the changes in java.io in java 8 ?
What is the statements?
What are the two ways in which thread can be created?
What are the 3 types of loops in java?