Unit 1-Chapter 3 - Classes, Objects and Methods
Unit 1-Chapter 3 - Classes, Objects and Methods
AND METHODS
UNIT 1 – Chapter 5
1.1 INTRODUCTION
• Classes and objects are core part of java programming
language.
01 02 03 04 05
All java programs A class is a It specifies both Java uses a class Thus, a class is
activity occurs template that the data and the to construct essentially a set
within a class. defines the form code that will objects and of plans and rules
of an object. operate on that objects are that specify how
data. instances of a to build an object.
class.
•There are two kinds of things we can include in a class definition,
• Data Members
• Methods
• Data Members: These are variables that store data items that typically differentiate one object of
the class from another.
• They are referred to as fields or member variables of a class.
• Methods: These define the operations you can perform for the class, they determine what we can
do with objects of the class.
• The fields in a class definition can be of any of the primitive data types, or they can be references
to objects of any class type.
• We specify the instance variables that it contains and the methods that operate on
them.
• A very simple class might contain only methods or only instance variables, but
most real-world classes contain both.
[ variable Declaration]
[ Method Declaration]
}
Public class Bicycle
{
/* Variable Declaration */
private int gear=5;
/* Method Declaration */
public void breaking()
{
System.out.println(“Working of Braking”);
}
}
Rules for defining a Class
If a class is to be defined
The name of the .java file A source file may have in a package, then the
must match with the any number of non-public package statement must
public class. classes. be the first statement in
the source file.
5.4 Adding
Variables to Class
Public class demo{
//declaring instance variables
Int age;
Float percentage;
//declaring class variables
Static int collegeCode;
CREATING
OBJECTS
• examples:
• Default Constructor
• No Argument Constructor
• Parameterised Constructor
Default Constructor
• If we do not create any constructor, the java compiler automatically create a no-
argument constructor during the execution of a program.
• We would not find default constructor in source code as it would be inserted into
the code during compilation and exists in .class file.
}
• The keyword this has several uses in java, the
keyword this refers to the current object.
int i, j;
A(){
A(int a){
}
• The process of calling the constructor
from other constructors is called as
constrictor chaining
METHOD OVERLOADING
The “Static” Keyword
• The static keyword in java is used to share the
same variable or method of a given class.
1. Variables
2. Methods
3. Blocks
4. Classes
a. Static Variables
• The instance variables are non-static and it is part of an object.
• But static variables are special type of variables that are not
associated with an object.
• Class demo{
int x, y;
static int z; }
• Static variables are initialized only once, at the start of the execution.
• A static variable can be accessed directly by the class name and doesn’t need
any object.
<class_name>.<variable_name>
demo.z;
b. Static Methods
• A static method is associated with a class rather than the instances.
• Static methods are appropriate when it is associated with a class rather than objects of that class.
Class staticDemo{
int x, y;
static int z;
system.out.println(z); } }
• A static method can be accessed directly by the class
name and doesn’t need any object.
<class_name>.<method_name>(arguments);
staticDemo.method1();
// variable initialization }
Example:
class Test{
// static variable
static int age;
// static block
static {
age=19;
}
}
d. Nested Static Class
1. Static Nested Classes