1. Classes and MethodsClasses and Methods
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Java and Data StructuresJava and Data Structures
2. Classes in Java
• A class is a user-defined data type with a template
that serves to define its properties.
• A class is declared by use of the class keyword.
• General form of class is:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class classname
{
type instance-variable1;
type instance-variable2;
//
type instance-variableN;
type methodName(parameter-list)
{
//body of method
}
}
3. Classes in Java
Field Declaration:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Rectangle
{
int length;
int width;
}
4. Classes in Java
Method Declaration:
• Methods are declared inside the body of the class but
immediately after the declaration of instance variable.
• The general form of method declaration id,
• Method declaration have four basic parts:
– Name of the method (methodName)
– Type of the value method return (type)
– List of parameters (parameter - List)
– Body of the method
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
type methodName (parameter-list)
{
method-body;
}
5. Classes in Java
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Rectangle
{
int length, width; // combined declaration
void getData (int x, int y)
{
length = x;
width = y;
}
int rectArea ()
{
int area = length*width;
return (area);
}
}
6. Creating objects
• Object is a block of memory that contains space to store all
the instance variable.
• Creating an objects is also called as instantiating an objects.
• Objects are created using new keyword.
• new operator creates an objects of the specified class and
returns a reference to that object.
• Example:
Rectangle rect; // Declare the objects
rect = new Rectangle(); // instantiating the objects
• Both statement combined into one as follows,
Rectangle rect = new Rectangle();
• Each objects has its own copy of instance variable.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
7. Accessing class members
• Using (.) operator we can access the class members i.e.
properties of class.
objectname.variablename = values;
objectname.methodname(parameterlist);
• Example:
rect.length = 10;
rect.width = 20;
rect.getData(10, 20);
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
8. Classes in Java
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Rectangle
{
int length, width; // combined declaration
void getData (int x, int y)
{
length = x;
width = y;
}
int rectArea ()
{
int area = length*width;
return (area);
}
}
class RectArea
{
public static void main (String arg[])
{
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
rect1.length = 10;
rect1.width = 10;
rect2.getData(10, 20);
System.out.println("Area 1: "+rect1.rectArea());
System.out.println("Area 2: "+ rect2.rectArea());
}
}
9. Constructors
• Constructors is a special type of method in java
which enables an object to initialize itself when it is
created.
• Constructors name is same as class name.
• They do not specify return type, not even void.
because it return the instance of class itself.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
10. Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Rectangle
{
int length, width; // combined declaration
Rectangle (int x, int y)
{
length = x;
width = y;
}
int rectArea ()
{
int area = length*width;
return (area);
}
}
class RectArea
{
public static void main (String arg[])
{
Rectangle rect = new (10, 20);
System.out.println("Area 1: "+ rect.rectArea());
}
}
11. this keyword
• this keyword can be used inside any method to refer
to the current class instance variable.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Rectangle (int length, int
width)
{
this.length = length;
this.width = width;
}
12. Method Overloading
• it is possible to create method that have the same
name, but different parameter lists and different
definitions.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
13. Nesting Method
• A method can be called only by its method name by
another method of same class.
• This is known as nesting of method.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
14. Static Members
• Like, static variable we can define static methods.
• In Java library, Math class defines many static
methods to perform math operation that can be
used in any program.
float x = Math.sqrt(25.0);
• Static methods called using class name.
• Static methods have several restriction:
– They can only call other static methods.
– They can only access static data.
– They can not refer to this or super in any program.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
15. Static Members
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class StaticDemo
{
static float mul(float x, float y)
{
return x*y;
}
static float divide(float x, float y)
{
return x/y;
}
}
class StaticMain
{
public static void main(String arg [])
{
float a = StaticDemo.mul(4.0, 5.0);
float b = StaticDemo.divide(a, 2.0);
System.out.println("Value of b: "+b);
}
}