In this article, you will learn the use of methods and constructors in java programming language.
A method may be defined as a set of statements or codes that can be invoked in order to perform certain operations and return certain values.
Also Read:- Exceptions in Java Programming Language
We have described below how to declare and call a method, use of the void keyword in a method, method overloading and command-line arguments which will give you a clear idea what methods can do in java programming language.
DECLARING A METHOD
To declare a method in java, the following syntax is used :
Syntax:
modifier returnType methodName(Arguments) {
//body of method
}
- modifier– this determines the type of access and controls the accessibility of the method within a class, outside a class, within a package etc. There are two types of modifiers:
- Access Modifiers (default, public, protected and private)
- Non-Access Modifiers (static, final, abstract, synchronized, transient, volatile, native and strictfp)
- returnType- the type of value to be returned. For Example: int, char, long, short, double, boolean, float.
- methodName- it represents the name of the method.
- Arguments- it represents the list of the arguments/parameters which contains data type and number of arguments.
- body of method- the method body contains statements which perform various operations.
Example :
public static int divisionExample(int a, int b) {
//code for division goes here
}
CALLING A METHOD
Whenever a call is made to a method, it took over the control of the program. As soon as the statement is executed or it reaches the closing curly braces, the called method delivers the control back to the caller.
Example :
class MaxNumber {
public static void main(String[] args) {
int x = 20, y = 10 ;
int z = maxNum(x, y) ;
System.out.println(“Larger Number = “ + z) ;
}
public static int maxNum(int a, int b) {
int max ;
if (a < b)
max = b ;
else
max = a ;
return max ;
}
}
Output:
Larger Number = 20
METHOD OVERLOADING
Method Overloading is the process of using same method name for two or more methods in a class or subclass but with different parameters.
It should not be confused with method overriding in which same method name is used with same datatype and same number of parameters.
We have shown below an example of method overloading in java programming language.
Example:
class Division1 {
static int divide (int x, int y) {
return x/y ;
}
}
class Division2 {
static float divide ( float x, float y ) {
return x/y ;
}
}
class MethodOverloading {
public static void main (String args[]) {
System.out.println(Division1.divide(30,20)) ;
System.out.println(Division2.divide(30,20)) ;
}
}
Output:
1
1.5
COMMAND-LINE ARGUMENTS
Command-line arguments are the information that are passed to the main() method of a program while running it through an array of strings. This enables a user to specify the application configuration when it is launched.
Example:
class CommandLineArguments {
public static void main(String args[]) {
for(int i = 0 ; i = args.length ; i++)
System.out.println(args[i]) ;
}
}
Compiling
javac CommandLineArguments.java
Running
java CommandLineArguments Amit Negi
Output:
Amit
Negi
CONSTRUCTORS IN JAVA
Constructors are used to initialize the objects. Their main function is to provide data to the created object. The name of the constructor and the class are same. The syntax is quite similar to a method and it does not have any explicit return type.
There are two types of constructors in java programming language.
- Default Constructor
- Parameterized Constructor
DEFAULT CONSTRUCTOR IN JAVA
A constructor that does not contain any parameters is called a Default Constructor. It should be noted that if a class does not have any constructor, the compiler will create the constructor automatically.
Syntax:
classname() {}
Example: An example of Default Constructor is shown below.
class W3TS {
W3TS () {
System.out.println(“Welcome to W3Training School”) ;
}
public static void main(String args[]){
W3TS obj = new W3TS () ;
}
}
Output: The following output will be generated.
Welcome to W3Training School
PARAMETERIZED CONSTRUCTORS IN JAVA
A constructor that contains parameters is known as Parameterized Constructors.
Example: We have shown below an example of parameterized constructor in java.
class W3TS {
String trainer ;
int salary ;
W3TS(String t, int s) {
trainer = t ;
salary = s ;
}
void details() {
System.out.println(trainer+” ” +salary ) ;
}
public static void main(String args[]){
W3TS obj1 = new W3TS (“Vikas”, 50000) ;
W3TS obj2 = new W3TS (“Amit”, 45000) ;
obj1.details() ;
obj2.details() ;
}
}
Output: The following output will be generated after executing the above java codes.
Vikas 50000
Amit 45000
USE OF THIS KEYWORD IN JAVA
In java programming language, this keyword is used to refer the current object in the current class. It can be used to refer variables, constructors and methods.
Additionally, it brings out the difference between instance variables and local variables when the two have similar names inside a method or constructor. For Example,
class W3TS {
int salary ;
W3TS(int salary) {
this.salary = salary ;
}
}
The keyword ‘this’ can also be used to invoke a constructor from the other constructor inside a class.
class W3TS {
int salary ;
W3TS() {
this(50000) ;
}
W3TS(int salary) {
this.salary = salary ;
}
}
Also Read: Nested Classes in Java Programming Language
We have provided you the best possible description on the use of methods and constructors in java programming language. Hope you like this article. For more updates and related information, stay connected to our blogs on java programming language.