0% found this document useful (0 votes)
4 views

7.core Java Methods

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

7.core Java Methods

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Leela Soft Core Java (J2SE) Madhusudhan

Methods in Java

Syntax: Method Declaration


<access_modifier> <return_type> <method_name>([parameters,...]) throws
<Exception_Class_Name>
{
stmts;
stmts;
stmts;

return <value/expression>;
}

String driving_licence()
{
return "AP102364";
}

String driving_licence(Date dob, String address)


{
return "AP102364";
}

public String driving_licence(Date dob, String address)


{
return "AP102364";
}

public String driving_licence(Date dob, String address)throws


InvalidAgeException
{
return "AP102364";
}

Method Calling:
<data_type> <return_value> = <method_name>([values,....]);

Examples:
mini_stmt();
welcome_msg("John");
double rem_bal = with_draw(2000);

Types of Methods:
1. Instance Method
2. Static Method

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

Ex:
class Test {
void m1() {
System.out.println("m1 instance method");
}

static void m2() {


System.out.println("m2 static method");
}

public static void main(String[] args) {


System.out.println("Main Method");
m2(); // method calling
Test.m2(); // method calling

Test t = new Test();


t.m1();
t.m2();

// m1();error: non-static method m1() cannot be referenced


from a static context
}
}

Ex:
class Test {
void m1() {
System.out.println("m1 instance method");
}

void m3() {
m1();
m2();
System.out.println("m3 instance method");
}

static void m2() {


System.out.println("m2 static method");
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t = new Test();
t.m1();
t.m2();
t.m3();
}

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

Method Parameters
class Test {
void m1(int x, double y) {
System.out.println("m1 instance method");
}

static void m2(int x, String y) {


System.out.println("m2 static method");
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t = new Test();
t.m1(10, 20.2);
t.m1(10, 'Y');
t.m1('S', 'Y');
Test.m2(100, "Java");
}
}

Ex:
class Test {
void m1(int x, double y) {
double z = x + y;
System.out.println("m1 instance method : " + z);
}

static void m2(int x, String y) {


System.out.println("m2 static method : " + x + " ## " + y);
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t = new Test();
t.m1(10, 20.2);
t.m1(10, 'Y');
t.m1('S', 'Y');
Test.m2(100, "Java");
}
}

Method Return Value:


class Test {
double m1(int x, double y) {

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

double z = x + y;
System.out.println("m1 instance method");

return z;
}

static void m2(int x, String y) {


System.out.println("m2 static method : " + x + " ## " + y);
}

public static void main(String[] args) {


System.out.println("Main Method");
double x1, y1, z1;

Test t = new Test();


x1 = t.m1(10, 20.2);
y1 = t.m1(10, 'Y');
z1 = t.m1('S', 'Y');

System.out.println("Value 1 : " + x1);


System.out.println("Value 2 : " + y1);
System.out.println("Value 3 : " + z1);
Test.m2(100, "Java");

}
}

Method Parameters Vs Non-primitive:


class Emp {
void empInfo() {
System.out.println("Employee Class");
}
}

class Cust {
void custInfo() {
System.out.println("Customer Class");
}
}

class Test {
static void m1(Emp x, Cust y) {
System.out.println("m1 instance method");
x.empInfo();
y.custInfo();
}

static void m2(int x, String y) {


System.out.println("m2 static method : " + x + " ## " + y);

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

public static void main(String[] args) {


System.out.println("Main Method");
Emp e = new Emp();
Cust c = new Cust();

Test t = new Test();


t.m1(e, c);
}
}

Method return type Vs Non-primitive:


class Emp {
void empInfo() {
System.out.println("Employee Class");
}
}

class Cust {
void custInfo() {
System.out.println("Customer Class");
}
}

class Test {
static Cust m1() {
System.out.println("m1 instance method");
Cust c = new Cust();

return c;
}

static Emp m2() {


System.out.println("m2 static method ");
Emp e = new Emp();

return e;
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t = new Test();
Cust c1 = t.m1();
Emp e1 = Test.m2();

c1.custInfo();
e1.empInfo();

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

}
}

[email protected] Cell: 78 42 66 47 66

You might also like