OOPJ-1.3 E-Learning PowerPoint Templates
OOPJ-1.3 E-Learning PowerPoint Templates
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
03 Control Structures
Looping, Methods,overloading,
04
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