0% found this document useful (0 votes)
11 views21 pages

2.2 ppt java

This document covers various aspects of methods and constructors in Java programming, including method declaration, types, advantages, and the distinction between methods and constructors. It explains how to create methods, pass parameters, and the significance of constructors in object initialization. Additionally, it discusses object passing techniques and provides examples to illustrate these concepts.

Uploaded by

sneha chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views21 pages

2.2 ppt java

This document covers various aspects of methods and constructors in Java programming, including method declaration, types, advantages, and the distinction between methods and constructors. It explains how to create methods, pass parameters, and the significance of constructors in object initialization. Additionally, it discusses object passing techniques and provides examples to illustrate these concepts.

Uploaded by

sneha chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

JAVA PROGRAMMING

(UNIT 2)

Mrs. Sneha D.Patil


(Assistant Professor, E&C)
INDEX
 What is method?  Advantages of
 Method declaration method
 Naming a method
 Constructor
 Types of method
 Method vs
constructor
 Method calling
 Types of constructor
 Ways to create
method in Java
 Argument passing
in constructor
 Method signature  Returning object
 Passing Parameters through methods
to a method  Types of object
passing
WHAT IS METHOD?
 A method is a block of code or collection of
statements or a set of code grouped together
to perform a certain task or operation.
 It is used to achieve the reusability of code.

 We write a method once and use it many

times.
 We do not require to write code again and

again. It also provides the easy


modification and readability of code, just
by adding or removing a chunk of code.
 The method is executed only when we call or

invoke it.
METHOD DECLARATION
NAMING A METHOD

 While defining a method, remember that the


method name must be a verb and start with
a lowercase letter. If the method name has more
than two words, the first name must be a verb
followed by adjective or noun. In the multi-word
method name, the first letter of each word must
be in uppercase except the first word. For
example:
 Single-word method name: sum(), area()
 Multi-word method name: areaOfCircle(),
stringComparision()
 It is also possible that a method has the same
name as another method name in the same
class, it is known as method overloading.
TYPES OF METHOD
METHOD CALLING

 The method needs to be called for use its


functionality. There can be three situations
when a method is called:
A method returns to the code that invoked it
when:
 It completes all the statements in the

method.
 It reaches a return statement.

 Throws an exception.
WAYS TO CREATE METHOD IN
JAVA

1. Instance Method: 2. Static Method:


Access the instance Access the static data
data using the object using class name.
name. Declared inside Declared inside
a class.
class
 Syntax:
with static keyword.
// Instance Method  Syntax:
void method_name(){
body // instance area //Static Method
} static void
method_name(){
body // static area
}
METHOD SIGNATURE:

 It consists of the method name and a


parameter list (number of parameters, type
of the parameters, and order of the
parameters). The return type and exceptions
are not considered as part of it.
 Method Signature of the above function:

 max(int x, int y) Number of parameters is 2,

Type of parameter is int.


PASSING PARAMETERS TO A
METHOD

 There are some cases when we don’t know


the number of parameters to be passed or an
unexpected case to use more parameters
than declared number of parameters. In such
cases we can use
 Passing Array as an Argument

 Passing Variable-arguments as an Argument

 Method Overloading.
ADVANTAGES OF USING
METHODS
 Reusability : Methods allow you to write code once and use it many times,
making your code more modular and easier to maintain.
 Abstraction : Methods allow you to abstract away complex logic and provide
a simple interface for others to use. This makes your code more readable and
easier to understand.
 Improved readability : By breaking up your code into smaller, well-named
methods, you can make your code more readable and easier to understand.
 Encapsulation : Methods allow you to encapsulate complex logic and data,
making it easier to manage and maintain.
 Separation of concern s: By using methods, you can separate different parts
of your code and assign different responsibilities to different methods,
improving the structure and organization of your code.
 Improved modularity : Methods allow you to break up your code into
smaller, more manageable units, improving the modularity of your code.
 Improved testability : By breaking up your code into smaller, more
manageable units, you can make it easier to test and debug your code.
 Improved performance: By organizing your code into well-structured
methods, you can improve performance by reducing the amount of code that
needs to be executed and by making it easier to cache and optimize your
code.
CONSTRUCTOR
 Java constructors or constructors in Java is a
terminology used to construct something in
our programs. A constructor in Java is
a special method that is used to initialize
objects. The constructor is called when an
object of a class is created. It can be used to
set initial values for object attributes.
WHEN JAVA CONSTRUCTOR IS
CALLED?

 Each time an object is created using


a new() keyword, at least one constructor (it
could be the default constructor) is invoked to
assign initial values to the data members of the
same class.
 Rules for writing constructors are as
follows:
 The constructor(s) of a class must have the same
name as the class name in which it resides.
 A constructor in Java can not be abstract, final,
static, or Synchronized.
 Access modifiers can be used in constructor
declaration to control its access i.e which other
class can call the constructor.
METHOD VS CONSTRUCTOR
TYPES OF CONSTRUCTER
EXAMPLE OF CONSTRUCTOR
public class Main  Output:
{
int x;
5
public Main(int y)
{
x = y;
}
public static void main(String[]
args)
{
Main myObj = new Main(5);
System.out.println(myObj.x);
}
}
ARGUMENT PASSING IN
CONSTRUCTOR
public class Main  Output:
{
int modelYear;  2000 Sneha
String modelName;
public Main(int year, String name)
{
modelYear = year;
modelName = name;
}
public static void main(String[]
args)
{
Main myCar = new Main(2000,
“Sneha");
System.out.println(myCar.modelY
ear + " " + myCar.modelName);
}
}
RETURNING OBJECT THROUGH
METHODS
class Calculator {

 Outpu
int result;
Calculator(int initialValue) {
this.result = initialValue;
}

t:
public Calculator add(int value) {
this.result += value;
return this;
}
public Calculator subtract(int value) {

Final
this.result -= value;
return this;
}
}
public class ObjectReturningExample

resul
{
public static void main(String[] args) {
Calculator calculator = new Calculator(
10);
int finalResult = calculator.add(5).subt

t: 12
ract(3).rsult;
System.out.println("Final result: " + fi
nalResult)
}
}
TYPES OF OBJECT PASSING
 Pass by Value
 Output:
public class PassByValueExample {
public static void modifyValue
Before modification:
(int value) 10
{
value = 20; After modification: 10
}
public static void main(String[] ags
){
int x = 10;
System.out.println("Before modi
fication: " + x);

modifyValue(x);
System.out.println("After modifi
cation: " + x);
}
}
PASS BY REFERENCE
PASSING OBJECTS THROUGH CONSTRUCTORS

class Box { public class ModifiedObjectInitialization {


double boxWidth, boxHeight, boxDept public static void main(String args[]) {
h;
Box(Box otherBox) { Box originalBox = new Box(10, 15, 25);
boxWidth = otherBox.boxWidth;
boxHeight = otherBox.boxHeight; Box clonedBox = new Box(originalBox);
boxDepth = otherBox.boxDepth; double volume;
}
Box(double width, double height, doub volume = originalBox.calculateVolume();
le depth)
{ System.out.println("Volume of originalB
boxWidth = width; ox is " +volume);
boxHeight = height;
boxDepth = depth; volume = clonedBox.calculateVolume();
}
System.out.println("Volume of clonedBo
// Compute and return the volume of t
x is " + volume);
he box
}
double calculateVolume() {
}
return boxWidth * boxHeight * boxD
epth;
Output:
}
Volume of the originalBox is 3750.0 Volume
}
ofclonedBox is 3750.0
TO BE CONTINUE……

You might also like