Method in Java is similar to a function defined in other programming languages. Method describes behavior of an object. A method is a collection of statements that are grouped together to perform an operation.
For example, if we have a class Human, then this class should have methods like eating(), walking(), talking() etc, which describes the behavior of the object. Declaring method is similar to function.
Syntax:
return-type methodName(parameter-list)
{
//body of method
}
return-type refers to the type of value returned by the method. methodName is a valid meaningful name that represent name of a method.
parameter-list represents list of parameters accepted by this method.
Method may have an optional return statement that is used to return value to the caller function.
Ex:
public String getName(String st)
{
String name="google";
name=name+st;
return name;
}
modifier : Modifier are access type of method. We will discuss it in detail later.
return Type : A method may return value. Data type of value return by a method is declare in method heading.
method name : Actual name of the method.
parameter : Value passed to a method.
method body : collection of statement that defines what method does.
Calling a Method
Methods are called to perform the functionality implemented in it. We can call method by its name and store the returned value into a variable.
String val = getName(".com")
It will return a value google.com after appending the argument passed during method call.
Returning Multiple values
In Java, we can return multiple values from a method by using array. We store all the values into an array that want to return and then return back it to the caller method. We must specify return-type as an array while creating an array.
Ex:
class MethodDemo2
{
static int[] total(int a, int b)
{
int[] s = new int[2];
s[0] = a + b;
s[1] = a - b;
return s;
}
public static void main(String[] args)
{
int[] s = total(200, 70);
System.out.println("Addition = " + s[0]); System.out.println("Subtraction = " + s[1]);
}
}
Output:
I:\>javac MethodDemo2.java
I:\>java MethodDemo2
Addition = 270
Subtraction = 130
Constructor
A Constructor is a block of code that initializes the newly created object. It is called when an instance(Object) of the class is created, it doesn’t have a return type. At the time of calling constructor, memory for the object is allocated in the memory. Constructor has same name as the class.
Note:
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least one constructor is called.
Rules for creating Java constructor
Constructor name must be the same as its class name
A Constructor must have no explicit return type
A Java constructor cannot be abstract, static, final, and synchronized Syntax:
public class MyClass
{
//This is the constructor
MyClass()
{
//code
}
..
}
Types of Java constructors
There are two types of constructors in Java:
Default constructor (no-arg constructor)
Parameterized constructor
Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax:
class_name()
{
//code
}
Ex1 on Default constructor:
Here, we are creating a no-arg constructor in the Bike class. It will be invoked at the time of object creation.
//Java Program to create and call a default constructor
class Bike1
{
//creating a default constructor
Bike1()
{
System.out.println("Bike is created");
}
//main method
public static void main(String args[])
{
//calling a default constructor
Bike1 b=new Bike1();
}
}
Output:
Bike is created
Ex2:
class Student3
{
int id;
String name;
//method to display the value of id and name
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
}
}
Output:
0 null
0 null
Parameterized Constructor
A constructor which has a specific number of parameters is called a parameterized constructor.
Ex:
class Student4
{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n)
{
id = i;
name = n;
}
//method to display the values
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
//creating objects and passing values
Student4 s1 = new Student4(111,"Ajay");
Student4 s2 = new Student4(222,"Vijay");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Output:
I:\>javac Main.java
I:\>java Student4
111 Ajay
222 Vijay
** Find Error in the following example
class Example3
{
private int var;
public Example3(int num)
{
var=num;
}
public int getValue()
{
return var;
}
public static void main(String args[])
{
Example3 myobj = new Example3();
System.out.println("value of var is: "+myobj.getValue());
}
}
Output:
C:\Users\KANAKA RAJU\Desktop>javac Example3.java
Example3.java:14: cannot find symbol
symbol : constructor Example3()
location: class Example3
Example3 myobj = new Example3();
^
1 error
No comments:
Post a Comment