Difference between revisions of "Java/C2/Methods/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 289: Line 289:
 
|-
 
|-
 
| 07:14
 
| 07:14
| Then the control jumps back to the '''Main'''  console.
+
| Then the control jumps back to the '''Main'''  function.
  
 
|-
 
|-

Revision as of 12:26, 1 January 2014

Time' Narration


00:02 Welcome to the Spoken Tutorial on methods in java.
00:06 In this tutorial we will learn
00:08 To create a method.
00:10 And To call a method.
00:13 Here we are using
00:14 Ubuntu version 11.10
00:17 Java Development kit 1.6 and
00:20 Eclipse 3.7.0


00:24 To follow this tutorial you must know how to write, compile and run a simple java program in eclipse.


00:32 If not, for relevant tutorials please visit our website which is as shown,

(http://www.spoken-tutorial.org)

00:40


A java method is a collection of statements that performs a specified operation.
00:46 Let us now write a method.


00:50 So, in the eclipse, I have already created a project Methods.
00:57 In the project, I have created a java class name MethodDemo.
01:06 In the class outside the main method we will write a method.


01:13 So type void name of the method
01:19 Let us name it as displayMessage parentheses Enter
01:29 And curly brackets.
01:32 A method can return a value.
01:34 But if you don’t want the method to return a value then the keyword voidis used.
01:42 Alright now inside the curly brackets, let us print a message.
01:47 So type System dot out dot println Hello Method.
02:06 So we have written a method.
02:10 Now we will call this method.
02:13 So inside the Main method, we will create an object of the classMethodDemo.
02:21 So MethodDemo object name.
02:26 Let's name it as md =new MethodDemo parentheses, semicolon.
02:37 So we have created an object mdof the class MethodDemo using the New operator.
02:48 Now let us call the method displayMessage.
02:51 So type md dot displayMessage


03:00 The Dot operator is used to call the method.
03:06 Now let us Run this application by clicking on Runicon.
03:14 We see the output Hello Method on the console
03:20 Now let us return an integer instead ofvoid.
03:26 So type int.
03:32 Also make the method public, that is accessible everywhere.
03:37 By default it is private, that is accessible only within the class where it is written.


03:45 Now inside the method type return seven, semicolon.
03:55 Remember that we write the return statement at the end of all in the method.
04:02 This is because after return statement no other statements are executed.
04:08 Now inside the Main method at the end type the print statement.
04:15 So type System dot out dot println();
04:23 Within parenthesis we will call the method.
04:28 So put md dot method inside the parentheses remove the semi-colon.
04:37 This will print the return value of the method.
04:42 Run the application.
04:45 We see in the output, the value 7 is printed.
04:51 Now we will write another method and call this methd in displayMessage.
04:59 So type public void method name square within parentheses int a.
05:15 Here we are giving int a as a parameter to our method.
05:20 Now within curly brackets type, System dot out dot println within parentheses a into a.
05:37 So we have written a square method.
05:40 That will display a square of an integer which is given as a parameter.
05:48 Let us call this method in the displayMessage method.
05:53 So type square within parentheses an integer 5, semicolon.
06:07 Run this application.
06:12 We see that the output displays the square of 5 that is 25.
06:19 Now let us understand the flow of the application.
06:24 The starting point is the Main method.
06:29 In the Main method, we have first called the displayMessage.
06:34 So the control goes to the displayMessage.
06:40 And all the statements in the displayMessage are executed.
06:45 The first one is the print statement.
06:50 Then it comes across the square method.
06:54 So the control jumps to the square method.
06:57 The square method takes an integer 5 and returns the square of the integer i.e. 25.
07:06 Then the control goes back to the displayMessage.
07:10 And it returns the value 7.
07:14 Then the control jumps back to the Main function.
07:20 Since there are no statements left to execute, in the main method the application terminates
07:29 Alright now let us make displayMessage as static.
07:35 So after public type static.
07:40 We see that we cannot call a non static method inside the static method
07:47 So we will comment this call.
07:52 Since Main is a static method, we can call the static displayMessage inside this
08:02 Now for static method we do not need to create an object.
08:07 So we will comment this object creation.
08:11 Also we will Delete md.
08:18 Run the application.
08:22 We see the output Hello Method and 7.
08:27 We do not see 25 because we have commented the call to square method
08:34 We can also call method from other class.
08:38 For that I have created a class Demo.
08:45 Inside the class create a method.
08:48 So type public void show parentheses Enter
08:56 Inside curly brackets, System dot out dot println
09:07 I am from other class.


09:13 Save the file.
09:16 Go back to method MethodDemo class
09:19 Now we will call this show method inside the method MethodDemo class.
09:28 For that we need to create the object of the class Demo.
09:22 This is because the show method belongs to the class Demo.
09:38 So type Demo d=new Demo parentheses, semicolon
09:48 Then call the method show parentheses.
09:54 Lets Run this application.
09:58 We see on the console I am from other class.
10:04 This is how methods are used in java.
10:09 The method name and the parameters forms the signature of the method.
10:14 While the curly brackets and the statements forms the body of the method.
10:23 So in this tutorial, we have learnt
10:25 To create a method
10:27 To call a method
10:29 And Different signatures of methods


10:32 For self assessment, create a method which prints the cube of an integer.
10:38 To know more about the Spoken Tutorial Project
10:41 Watch the video available at http://spoken-tutorial.org/What_is_a_Spoken_Tutorial
10:47 It summarizes the Spoken Tutorial project
10:50 If you do not have good bandwidth, you can download and watch it


10:54 The Spoken Tutorial Project Team
10:56 Conducts workshops using spoken tutorials
10:58 Gives certificates to those who have pass an online test
11:02 For more details, please write to contact@spoken-tutorial.org


11:08 Spoken Tutorial Project is a part of the Talk to a Teacher project
11:12 It is supported by the National Mission on Education through ICT, MHRD, Government of India
11:18 More information on this Mission is available at
http://spoken-tutorial.org/NMEICT-Intro 


11:27 We have come to the end of this tutorial.
11:29 Thanks for joining.
11:30 This is Prathamesh Salunke signing off.

Jai Hind.

Contributors and Content Editors

Arya Ratish, Gaurav, Jyotisolanki, PoojaMoolya, Priyacst, Sandhya.np14, Sneha