Methods in Java
Methods in Java
What is Method?
A method is a set of code which is referred to by name and can be called
(invoked) at any point in a program simply by utilizing the method's name.
Why Method?
When program becomes more and more complex i.e. .When they gain in size. So one
of the most powerful techniques to cope with complexity in computer programs is
affectionately known as “Divide and Conquer” - that is to take a complex task and
divide it into smaller tasks and they are implemented by Methods
One of the most powerful techniques to reduce this complexity is “Divide and
Conquer” which is to take a complex task and divide it into smaller and easily
understandable tasks. In Java, we accomplish this technique with the help of methods .
Method signatures
The line where the methods parameters and return type are defined is known as the method
signature.
public void calculate()
public double calculate()
public void calculate (double number1, double number2)
public double calculate(double number1, double number2)
Creating Method:
Parameter List: The list of parameters, it is the type, order, and number of
parameters of a method. These are optional, method may contain zero
parameters.
Method body: The method body defines what the method does with
statements.
Example:
Here is the source code:
}
Method Calling:
For using a method, it should be called.
This called method then returns control to the caller in two conditions, when:
The methods returning void is considered as call to a statement. Let’s consider an example:
System .out.println("This is tutorialspoint.com !");
}
Method with return Type
}
static and non-static method in Java
A static method is a method that belongs to a class, but it does not belong to
an instance of that class and this method can be called without the instance or
object of that class.
Following are the important differences between static and non-static method.
Access A static method can access A non-static method can access both static as
only static members and well as non-static members.
1
cannot access non-static
members.
Binding Static method uses compile Non-static method uses run time binding or
2 time binding or early dynamic binding.
binding.
Memory Static method occupies less A non-static method may occupy more space.
allocation space and memory Memory allocation happens when method is
4
allocation happens once. invoked and memory is deallocated once
method is executed completely.
Keyword A static method is declared A normal method is not required to have any
5
using static keyword. special keyword.
Compile time vs Runtime
Compile-time and Runtime are the two programming terms used in the software
development. Compile-time is the time at which the source code is converted into an
executable code while the run time is the time at which the executable code is started
running. Both the compile-time and runtime refer to different types of error.
Compile-time errors
Compile-time errors are the errors that occurred when we write the wrong syntax. If we
write the wrong syntax or semantics of any programming language, then the compile-
time errors will be thrown by the compiler. The compiler will not allow to run the
program until all the errors are removed from the program. When all the errors are
removed from the program, then the compiler will generate the executable file.
o Syntax errors
o Semantic errors
Syntax errors
When the programmer does not follow the syntax of any programming language, then
the compiler will throw the syntax error.
For example,
int a, b:
The above declaration generates the compile-time error as in Java, every statement
ends with the semicolon, but we put a colon (:) at the end of the statement.
Semantic errors
The semantic errors exist when the statements are not meaningful to the compiler.
For example,
a+b=c;
The above statement throws a compile-time errors. In the above statement, we are
assigning the value of 'c' to the summation of 'a' and 'b' which is not possible in C
programming language as it can contain only one variable on the left of the assignment
operator while right of the assignment operator can contain more than one variable.
c=a+b;
Runtime errors
The runtime errors are the errors that occur during the execution and after compilation.
The examples of runtime errors are division by zero, etc. These errors are not easy to
detect as the compiler does not point to these errors.
Compile-time Runtime
The compile-time errors are the errors The runtime errors are the errors which are not
which are produced at the compile-time, generated by the compiler and produce an
and they are detected by the compiler. unpredictable result at the execution time.
In this case, the compiler prevents the In this case, the compiler does not detect the
code from execution if it detects an error error, so it cannot prevent the code from the
in the program. execution.
It contains the syntax and semantic errors It contains the errors such as division by zero,
such as missing semicolon at the end of determining the square root of a negative
the statement. number.
Good programmers write in a modular fashion which allows for several programmers
to work independently on separate concepts which can be assembled at a later date to
create the entire project. The use of methods will be our first step in the direction of
modular programming.
Methods are time savers, in that they allow for the repetition of sections of code
without retyping the code. In addition, methods can be saved and utilized again and
again in newly developed programs.