0% found this document useful (0 votes)
48 views8 pages

OOPJ-1.3 E-Learning PowerPoint Templates

Uploaded by

Heer Patel
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)
48 views8 pages

OOPJ-1.3 E-Learning PowerPoint Templates

Uploaded by

Heer Patel
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/ 8

Object Oriented

Programming with
Java(CT405-N)
Prof. Vimal Bhatt , Assistant
Professor
Computer Science & Engineering
Vidush Somany Institute of Technology & Research,Kadi
Chapter 1
Introduction to Java
Agenda 01 Basic of Java Programming

Style 02 Data types ,Variables, operators

03 Control Structures

Looping, Methods,overloading,
04

05 Math class, Array in Java


What is a Method in Java?
What is Method? Method Declaration
 A method is a block of code or collection of statements
The method declaration provides information about method
or a set of code grouped together to perform a certain
attributes, such as visibility, return-type, name, and arguments. It
task or operation. It is used to achieve the reusability of
has six components that are known as method header, as we have
code. We write a method once and use it many times.
shown in the following figure.
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 Signature: Every method has a method signature. It is a part of the method declaration.
It includes the method name and parameter list.
Access Specifier: Access specifier or modifier is the access type of the method. It specifies the
visibility of the method. Java provides four types of access specifier:
•Public: The method is accessible by all classes when we use public specifier in our application.
•Private: When we use a private access specifier, the method is accessible only in the classes in
which it is defined.
•Protected: When we use protected access specifier, the method is accessible within the same
package or subclasses in a different package.
•Default: When we do not use any access specifier in the method declaration, Java uses default
access specifier by default. It is visible only from the same package only.
Return Type: Return type is a data type that the method returns. It may have a primitive data
type, object, collection, void, etc. If the method does not return anything, we use void keyword.
What is a Method in Java?
What is Method? Predefined Method
 Method Name: It is a unique name that is used to define the name of a  In Java, predefined methods are the method that is already defined in the Java class libraries is
method. It must be corresponding to the functionality of the method. known as predefined methods. It is also known as the standard library method or built-in
Suppose, if we are creating a method for subtraction of two numbers, the method. We can directly use these methods just by calling them in the program at any point.
method name must be subtraction(). A method is invoked by its name. Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc. When we call
 Parameter List: It is the list of parameters separated by a comma and any of the predefined methods in our program, a series of codes related to the corresponding
enclosed in the pair of parentheses. It contains the data type and variable method runs in the background that is already stored in the library.
name. If the method has no parameter, left the parentheses blank.  Each and every predefined method is defined inside a class. Such as print() method is defined
 Method Body: It is a part of the method declaration. It contains all the in the java.io.PrintStream class. It prints the statement that we write inside the method. For
actions to be performed. It is enclosed within the pair of curly braces. example, print("Java"), it prints Java on the console.
 Let's see an example of the predefined method.

User-defined Method
public class Demo 1.user defined method
{ 2.public static void findEvenOdd(int num)
public static void main(String[] args) 3.{
{
1.// using the max() method of Math class 4.//method body
System.out.print("The maximum number is: " + Math.max(9,7)); 5.if(num%2==0)
} 6.System.out.println(num+" is even");
}
Output: 9
7.else
8.System.out.println(num+" is odd");
9.}
Static Method

A method that has static keyword is known as static method. In other words, a
method that belongs to a class rather than an instance of a class is known as a
How to Call or Invoke a User-defined Method static method. We can also create a static method by using the
keyword static before the method name.
Once we have defined a method, it should be called. The
The main advantage of a static method is that we can call it without creating an
calling of a method in a program is simple. When we call or object. It can access static data members and also change the value of it. It is
invoke a user-defined method, the program control transfer used to create an instance method. It is invoked by using the class name. The
to the called method. best example of a static method is the main() method.
Example of static method
Display.java
public class Display
import java.util.Scanner;
{
public class EvenOdd public static void main(String[] args)
{ {
public static void main (String args[]) show();
{ }
//creating Scanner class object static void show()
{
Scanner scan=new Scanner(System.in); System.out.println("It is an example of static method.");
System.out.print("Enter the number: "); }
//reading value from the user }
int num=scan.nextInt();
//method calling
findEvenOdd(num);
}
Instance Method Static Method

Instance Method A method that has static keyword is known as static method. In other words, a
The method of the class is known as an instance method. It is a non-static method method that belongs to a class rather than an instance of a class is known as a
defined in the class. Before calling or invoking the instance method, it is necessary static method. We can also create a static method by using the
to create an object of its class. Let's see an example of an instance method. keyword static before the method name.
InstanceMethodExample.java The main advantage of a static method is that we can call it without creating an
public class InstanceMethodExample object. It can access static data members and also change the value of it. It is
{ used to create an instance method. It is invoked by using the class name. The
public static void main(String [] args) best example of a static method is the main() method.
{ Example of static method
1.//Creating an object of the class Display.java
InstanceMethodExample obj = new InstanceMethodExample(); public class Display
2.//invoking instance method {
System.out.println("The sum is: "+obj.add(12, 13)); public static void main(String[] args)
} {
int s; show();
3.//user-defined method because we have not used static keyword }
public int add(int a, int b) static void show()
{ {
s = a+b; System.out.println("It is an example of static method.");
4.//returning the sum }
5.return s; }
}
}
THANK YOU

You might also like