Académique Documents
Professionnel Documents
Culture Documents
Circle aCircle;
Circle bCircle;
Class of Circle cont.
aCircle, bCircle simply refers to a Circle
object, not an object itself.
aCircle bCircle
null null
bCircle = aCircle;
Creating objects of a class
aCircle = new Circle();
bCircle= new Circle() ;
bCircle = aCircle;
P Q P Q
Automatic garbage collection
Q
The object does not have a reference and
cannot be used in future.
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
double area;
aCircle.r = 1.0;
area = aCircle.area();
Using Circle Class
// Circle.java: Contains Circle class
class MyMain
{
public static void main(String args[])
{
Circle aCircle; // creating reference
aCircle = new Circle(); // creating object
aCircle.x = 10; // assigning value to data field
aCircle.y = 20;
aCircle.r = 5;
double area = aCircle.area(); // invoking method
double circumf = aCircle.circumference();
System.out.println("Radius="+aCircle.r+" Area="+area);
System.out.println("Radius="+aCircle.r+" Circumference ="+circumf);
}
}
Constructors
Defining Constructors
A constructor is a special kind of method that is
designed to perform initializations, such as giving
values to instance variables.
A constructor is a method that is called when a
new object is created.
A constructor can perform any action you want,
but it is meant to perform initializations.
Constructors serve very much the same purpose as
set methods.
Defining Constructors (cont’d)
Unlike set methods, constructors are called
automatically whenever you create an object
using the new operator.
Constructors have the same name as the class.
Constructors are normally overloaded so there
are multiple definitions of the constructor, each
with different numbers or types of parameters.
Differences between Constructor and
Set Methods
Headings of constructor methods do not have
the word void. They do not return anything,
but unlike other methods that don’t return
values, the word void is omitted.
Unlike set methods, the constructors give values
to all the instance variables even though there
may not be an argument for each instance
variable.
Differences between Constructor and
Set Methods (cont’d)
Whenever you define at least one
constructor, you should be sure to
include a constructor with zero
parameters. Such a constructor is called
a default constructor.
Using Constructors
Constructors are called at the time that you use
new to create an object.
Whenever a class definition does not have a
constructor definition, Java automatically creates a
default constructor, that is, one with zero
parameters.
However, once you add at least one constructor,
then you are in charge of all constructors, i.e., no
default constructor is created by Java.
Using Constructors (cont’d)
When you create a new object with the operator
new, you must always include a call to a
constructor after the operator new.
public PersonRecord( )
{
name = "No name yet.";
age = 0;
weight = 0;
}
Public PersonRecord(String initialName, int initialAge,double initialWeight)
{
name = initialName;
if ((initialAge < 0) || (initialWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = initialAge;
weight = initialWeight;
}
}
public PersonRecord(int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = initialAge;
}
Public PersonRecord(double initialWeight)
{
name = "No name yet";
age = 0;
if (initialWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = initialWeight;
}
public void set(String newName, int newAge, double newWeight)
{
name = newName;
if ((newAge < 0) || (newWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = newAge;
weight = newWeight;
}
}
public void set(int newAge)
{
if (newAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge;
//name and weight are unchanged.
}
public void set(double newWeight)
{
if (newWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = newWeight; //name and age are unchanged.
}
public String getName( )
{
return name;
}
public int getAge( )
{
return age;
}
public double getWeight( )
{
return weight;
}
}
import java.io.*;
class PersonRecordDemo
{
public static void main(String[] args)
{