Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

How do you sort arraylist in descending order?

1021


Is break statement can be used as labels in java?

965


What is dynamic array in java?

1028


What are the methods used to implement for the key object in the hash map?

1043


Is there a way to increase the size of an array after its declaration?

1055


Why do we use threads in java?

1120


Is it safe to install java on my computer?

1143


Difference between predicate, supplier and consumer ?

1061


How can you make a class serializable in java?

988


What does the append?

1001


What is a Hash Table? What are the advantages of using a hash table?

1088


Explain the private protected method modifier?

987


Explain enumeration in java?

1009


What lambda means?

1005


Can you sort a list in java?

982