|
CSE 115 COMMUNICATION TASK TUTORIAL#2-CALLING METHODS AND PARAMETERS PATEL VIRALKUMAR ASHOKBHAI. v Calling methods Ø
Method: Method is a java code that tells the object
what to do.
Object has
properties and capabilities. Using methods we can tell the object or send the
message to the object to perform a particular task. We can imagine a calculator
.It’s color, size, shape and all things that describe it are included in its
properties. But the actions like addition, subtraction, division,
multiplication, finding square root, storage of result in the memory are all
its capabilities .These actions are similar to methods in Java.
A constructor
is a special method which creates an instance of a class. It initializes the
object’s capabilities. It sets the initial stage of the object. If the user
does not define any constructor the Java automatically defines a constructor
which is called the default constructor. Ø
Constructor definition: 1. Constructor Header: public identifier ( ) Here identifier must have
the same name as the name of class. 2. Constructor Body: It is always
enclosed in curly braces. The collection of statements between the
braces is called a block. package vehicles; public class Car { public Car ( ) { } There are other methods also which are
different from the constructor. The method definition consists of the two
things: i. Method Header ii. Method Body i. Method Header: visibility returntype
methodname ( ) Here visibility is a
key word which is usually ‘public’. Method name is
usually in lower case but if it has multiple words then the subsequent letters
are in upper case. A method may or may
not have a return type. If it does not have a return type it is called void
(which is a keyword).* i. Method Body: It is always enclosed in curly braces. Consider
the example given below. public class Stars { public Stars ( ) {
}
public void
firstmethod ( ) {
}
public void
secondmethod ( ) {
} } In
the above example we have created two methods namely firstmethod and
secondmethod and both these methods have no return type. So we have written
‘void’ in the place of return type.
Ø
Syntax of method call: Objectname.methodname
( ); A method gets its
input from the information we pass in the argument list (between the
parenthesis).Expressions, variables, or values can be passed as arguments in
the argument list. There are same numbers of arguments in the method call as
well as method definition. If the parenthesis is empty, then it indicates that
the method does not have any arguments. By calling a method we actually invoke
or activate a particular capability of that object. Example: 1 public void showScore ( ); In the above example
we have created a method which calculates and displays the score but before
passing this information (code) one should define what does this method do. Example: 2 public void convertTemperature ( ); This method converts temperature from one unit to another. * The constructor method does not have a return type. Calling constructor: new constructorname ( ); Example: 1 new star.Supernova ( ); Here the name of the constructor is star.Supernova. The name of the constructor should be the same as the name of the class. v Parameters and
Arguments: There are actual parameters and formal parameters .Both of them is slightly different from one another. 1. Formal parameters: During method definition the variables which are used are called the formal parameters. Example: public void draw(Circle circle) { } Parameters in the above example are called the formal parameters. 2. Actual parameters: While invoking a method by calling a method the parameters which are passed into the method are called the actual parameters (arguments). Example: d.draw (circle); Parameter in the above example is the actual parameter. |