Professional Documents
Culture Documents
Methods and Constructors
Methods and Constructors
Methods and Constructors
Example:
public static void main (String args[]) Method Header
{
… Method Body
}
• Method header specifies the modifier, return type, method
name and parameters of method.
public void display()
{…}
"+acno);
}
public static void main(String args[])
{
Bank b1=new Bank();
Bank b2=new Bank();
Bank b3=new Bank();
}
}
if you wish to call a static method from outside its class, you
can do so using the following general form:
classname.method( )
• It is not mandatory for the coder to write constructor for the class.
Default Constructor
• If no user defined constructor is provided for a class,
compiler initializes member variables to its default
values.
– numeric data types are set to 0
– char data types are set to null character(‘’)
– reference variables are set to null
– boolean are set to false
// Data Fields…
// Constructor
public ClassName()
{
// Method Body Statements initialising
Data Fields
}
}
}
Let’s Do Some thing
Write a program to create a class named Patient which contains:
a. Attributes patient _name, age, contact_number
b.Constructor to initialize all the patient attributes
c. A method to display all the details of any patient
Create a Patient object and display all the details of that patient.
Future Work:
a. Assign unique patient_id to each patient automatically.
b.Store all the patient objects in an array or ArrayList.
c. Write a method to diplay all the details of a patient with a
given patient_id.
Singleton Design Pattern
Advantage:
Saves memory because object is not created at each request.
Only single instance is reused again and again.
Usage of Singleton Design Pattern