Friday, July 2, 2010

Java's Constructors

Constructors are made to create objects and initialize their values. Unlike other methods, constructor method carries the name of its class and does not return any value.

I have two programs below with one file containing the constructor while the  other one is calling the constructor file.

[+/-] show/hide

public class TestExpandedConstructor {
public static void main (String args[]){

EventSiteConstructor oneSite = new EventSiteConstructor(); // calling file with constructor

System.out.println("Name :"+oneSite.getManagerName());
System.out.println("Site Number: "+oneSite.getSiteNumber());
System.out.println("Usage Fee:"+oneSite.getUsageFee());

public class EventSiteConstructor {

private int siteNumber;
private double usageFee;
private String managerName;

EventSiteConstructor(){  //this is the constructor method

public int getSiteNumber()
{ return siteNumber;}

public void setSiteNumber(int n)
{ siteNumber=n;}

public void setUsageFee(double amt)
{ usageFee=amt;}

public double getUsageFee()
{ return usageFee;}
public String getManagerName()
{ return managerName;}

public void setManagerName(String name)
{ managerName=name;}


No comments: