WHAT IS A CLASS?
It is defined as a blueprint or a template from
which an object is declared
[access specifier] [modifiers] class <class name>
Members of the class
RULES FOR NAMING A CLASS
A class name should not be a keyword in java
Name can begin with letter, an _ or $
Name should not contain embedded space or
It can be of any length
WHAT IS AN OBJECT?
Its an instance of a class.
Its maintains its states in variables and
implements its behavior with its methods
<Class name><object name>;
Student obj=new Student();
MEMBERS OF A CLASS
of a class
Instance variables Class variables
HOW TO DECLARE METHODS?
int add(int a , int b)
HOW TO ACCESS THE MEMBERS?
ClassName.<member>; for static members
1. A constructor is a special function having the
same name that of the class.
2. It has no return type
3. And is invoked through new operator
//Body of constructor
Create class named student whose members
Name// to store student name
Roll //to store his roll no
Disp() // display name and roll
public class Student
public String name;
public int roll;
public void disp()
Accessible from all other
Inner classes cannot have
public access specifiers
Only one public class can
be declared in a source file
Only objects of the same class
can have access to private
This provides highest degree of
Subclasses cannot derived
Modifier Used on Meaning
Contains unimplemented methods and cannot be instantiated.
All interfaces are abstract. Optional in declarations
No body, only signature. The enclosing class is abstract
Cannot be subclassed
Cannot be overridden and dynamically looked up
Cannot change its value. static final fields are compile-time
Cannot change its value.
native method Platform-dependent. No body, only signature
All methods in the class are implicitly strictfp.
All floating-point computation done is strictly conforms to
the IEEE 754 standard. All values including intermediate
results must be expressed as IEEE float or double values.
It is rarely used.
Make an inner class top-level class
A class method, invoked through the class name.
A class field, invoked through the class name
one instance, regardless of class instances created.
Run when the class is loaded, rather than when an instance
synchronized method For a static method, a lock for the class is acquired before
executing the method. For a non-static method, a lock for
object instance is acquired.
transient field Not be serialized with the object, used with object
volatile field Accessible by unsynchronized threads, very rarely used.
Write a program in which when ever
an object is created of that class
it prints the no of objects created