Home »
Scala
Method Invocation in Scala
Method invocation is the technique to call a method in Scala programming language. In this tutorial on Scala method invocation, we will learn all legal methods to invoke the method.
Submitted by Shivang Yadav, on September 20, 2019
Scala Method Invocation
Method invocation is the legal and correct technique to call a method in Scala programming language. The methods of a class are usually accessed by using two methods.
- Object Creation
- Inheritance
We dynamically call methods of a class using the object of the class. the correct way of accessing the methods using object are:
Invoking methods using dot operator
While calling a method of a class from the object of the class we use the dot (.) operator. The correct and legal way of accessing the methods of a class in Scala using the dot operator is,
Syntax
object_name.method_name(parameters)
This is the most appropriate way of accessing the method of a class using an object. Else than this some ways can be used but are not ideal. These methods do not generate any error but are not correct ways to call the method. They are,
object_name . method_name(parameters)
object_name. method_name(parameters)
object_name . method_name (parameters)
Example
class car{
def sound(noise:String){
println(noise+" goes fast")
}
}
object MyClass {
def main(args: Array[String]) {
var newcar = new car();
newcar.sound("Lamborghini!")
newcar . sound("Honda!")
newcar .sound("Mercedes!")
newcar . sound ("BMW!")
}
}
Output
Lamborghini! goes fast
Honda! goes fast
Mercedes! goes fast
BMW! goes fast
Invoking methods directly
Calling a method from a class inherits the base class or in the same class. The correct and legal way to call the method is,
Syntax
method_name(parameters)
Other than this other methods are also legal. Like,
method_name (parameters)
method_name( parameters )
method_name ( parameters )
Example
object MyClass {
def sound(noise:String){
println(noise+" makes noise")
}
def main(args: Array[String]) {
sound("Lamborghini!")
sound ( "Honda!")
sound ( "Mercedes!" )
}
}
Output
Lamborghini! makes noise
Honda! makes noise
Mercedes! makes noise
Invoking methods with parameters
When the method contains arguments and calling methods with parameters. For invoking methods with the argument we will separate the parameters using commas and single space.
Syntax
object_name.method_name(parameter1, parameter2)
Other legal methods that are not correct but does not compile to an error,
object_name. method_name(parameter1, parameter2)
object_name.method_name (parameter1, parameter2)
object_name.method_name( parameter1 , parameter2 )
object_name. method_name ( parameter1 , parameter2 )
Example
class calculator{
def add(a:Int , b:Int){
println("The sum of "+a+" and "+b+" is "+(a+b))
}
}
object MyClass {
def main(args: Array[String]) {
var calc = new calculator();
calc.add(12,34)
calc. add(1,434)
calc.add (2,3)
calc.add(15, 4)
calc. add ( 232,314 )
}
}
Output
The sum of 12 and 34 is 46
The sum of 1 and 434 is 435
The sum of 2 and 3 is 5
The sum of 15 and 4 is 19
The sum of 232 and 314 is 546
Invoking special method
When a method in Scala does not accept any argument. So, while invoking no arguments are passed which make the parentheses optional. Without parenthesis, the code becomes much more readable and also easy the programmers work.
Both this is legal and correct ways to invoke a method that does not take any parameter.
Syntax
object_name.method_name()
object_name.method_name
Example
class car{
def sound(){
println("Broom Broom Brooooom!!!!!")
}
}
object MyClass {
def main(args: Array[String]) {
var newcar = new car();
newcar.sound()
newcar.sound
}
}
Output
Broom Broom Brooooom!!!!!
Broom Broom Brooooom!!!!!