Unit 2
Unit 2
Classes and
Objects
Class
A class is a user defined data type with a
template that serves to define its
properties.
null
length
Width
Accessing Class Members:
objectname.variablename = value;
objectname.methodname(paramater.list);
Introducing Methods:
A Java method is a set of Java
statements which can be included inside
a Java class.
Java methods are similar to
functions or procedures in other
programming languages.
General Form:
type name(parameter-list){
// body of method
}
Classification of Mehod
1. Based on nature of creation
1. User Defined
2. Predefined
2. Based on return type
1. Return a value
2. Return void
3. Based on method call
1. Nonrecursive
2. recursive
Constructors:
Constructor are special type of
method that enables an object to
initialize itself when it is created.
• Syntax:
– Protected void finalize()
{
// finalization code.
}
The Finalize Method
• finalize() is only called just prior to
garbage collection. It is not called
when an object goes out-of scope.
Final
There are three use of final.
}
Final
3. Using final to prevent Inheritance
To disallow a class from being
inherited, declare a class as a final
implicitly declared all of its methods as a
final, too.
Syntax:
final class class-name
{
}
WHAT IS STATIC FIELDS??..
• Static fields are also know as class
fields.
For Example:
static int x;
FEATURES OF STATIC FIELD
• Static variables are initialized to
default values when the class is
loaded into memory.
WHAT IS STATIC
METHOD??..
• A static method is a simple method
having static keyword as a modifier.
• Syntax:
static ReturnType
MethodName(parameters)
FEATURES OF STATIC METHOD
• static methods are created to access it
without any object instance.