Java/C3/Static-Methods/English-timed

From Script | Spoken-Tutorial
Revision as of 10:17, 18 May 2020 by Sandhya.np14 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
00:01 Welcome to the Spoken Tutorial on Static Methods.
00:05 In this tutorial we will learn: what are static methods , defining static methods.
00:12 difference between instance methods and static methods and how to use static methods.
00:20 Here we are using Ubuntu 14.04, JDK 1 .7 and Eclipse 4.3.1
00:31 To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.
00:38 You should also have the knowledge of instance variables, methods and static variables in Java.
00:45 If not, for relevant Java tutorials, please visit the link shown.
00:50 A static method is a method which is associated with the entire class.
00:56 It is also called a class method and is declared using the static keyword.
01:02 Static methods are usually used to handle static variables.
01:07 Now we will switch to Eclipse and create a new project called StaticMethodDemo.
01:14 Inside this project we will create the necessary classes to demonstrate the usage of Static methods.
01:21 We will create a new class StudentEnroll.
01:25 Let us see how to use static methods with an example.
01:30 The example is very similar to the one which is used in the Static Variable tutorial.
01:37 Here again we are representing the StudentEnroll class.
01:42 Recall that the variables name and id are handled as instance variables.
01:48 Here the variables organization and total count are common to the entire class.
01:54 So they can be treated as static variables.
01:58 Now type the following code to represent the StudentEnroll class.
02:03 Note that there are two static variables count and orgname.
02:08 Also note that orgname is not a static constant rather it is normal static variable.
02:15 The static variable orgname is initialized as “IIT Bombay”.
02:21 Now click on Source -> and select Generate Constructor using Fields.
02:27 Delete the super keyword from the generated code.
02:32 Inside the constructor, type count ++ semicolon So, the count value is incremented every time an object is created.
02:42 Now we will add a method showData( ) to this class to print the values of the variables.
02:48 So type public void showData( ).
02:51 Within brackets type the following code to print the values of id, name and organisation name.
02:58 Now we will add the static method setOrgName.
03:03 Type the following code.
03:05 The setOrgName method represented here is a static method which can modify the value of orgname.
03:13 Any method which is used to handle static variables can be defined as a static method.
03:19 Now let us explore the differences between instance method and static method.
03:25 Instance methods can access static variables.
03:29 Whereas a static method can directly access and modify only static variables.
03:35 Instance methods are invoked only by an object.
03:39 Whereas a static method can be invoked directly without creating an object.
03:45 We cannot use ‘this’ and ‘super’ keyword inside a static method.
03:50 This is because these keywords refer to the instance of a particular class.
03:56 In a static context, we can’t refer to instances of a class.
04:01 Let us see what happens if we try to access an instance variable directly inside this static method.
04:09 Type, id= “newid” semicolon
04:13 Now an error comes up in Eclipse.
04:17 It indicates that an instance variable cannot be accessed directly inside a static method.
04:23 So let us comment this line and proceed.
04:27 Now we will add one more static method showOrgData.
04:31 These statements print the values of orgname and count.
04:36 Now right click on the default package, click New-> Class and then type the name as Demo.
04:44 Inside this class we will have the main method.
04:48 So type main and then press Ctrl+space to generate the main method.
04:54 We will create a few objects of StudentEnroll class to represent student enrollments.
05:01 So type the following code to create 3 objects s1, s2 and s3.
05:08 Now let us invoke the showData method to print the enrollment details.
05:12 Type the following code to invoke showData method on s1, s2 and s3.
05:19 Let us also invoke the method showOrgData to print the values of orgname and count.
05:27 Since it is a static method we can invoke it directly using its class name.
05:31 To do so, type this code.
05:34 Now let us run the Demo program.
05:37 We can see that the values of the variables corresponding to s1 i.e IT101, ADIL and IIT BOMBAY gets printed.
05:47 Similarly the values corresponding to s2 and s3 are also printed.
05:53 Note that the value of orgname ie IIT BOMBAY is common for s1, s2 and s3.
06:02 orgname and count are printed separately by the static method showOrgData.
06:08 Note that the organisation name is printed as IIT Bombay.
06:13 The value of the number of student enrollments is printed as 3, as we have already created 3 objects.
06:21 A static method can be invoked directly by the class name itself.
06:26 Now let us invoke the static method setOrgName.
06:30 We will change the organisation name from “IIT Bombay” to “IIT Mumbai”.
06:36 So type the following code.
06:38 Now let us once again invoke the showData method on s1, s2 and s3 to print the enrollment details.
06:47 For that type the following code again.
06:50 Once again, let us invoke the method showOrgData to print the values of orgname and count.
06:58 To do so, type this code.
07:00 Now run the Demo program again.
07:03 We can see that the organisation name is changed to “IIT Mumbai”.
07:08 Let us come back to slides.
07:11 Object references can be passed to a static method.
07:15 This way a static method can access the instance variables of that particular object.
07:22 Let us try it in our code. Switch to Eclipse and go to the StudentEnroll class.
07:30 Now in the setOrgName method, pass another argument as an object of StudentEnroll class.
07:38 So after String org, type comma StudentEnroll s
07:45 Now inside this method, uncomment id = "newid"
07:50 And instead of id,type s.id
07:54 Now go to the Demo class.
07:56 Let us modify the function call to setOrgName method by passing the StudentEnroll object s1.
08:05 So here, after “IIT Mumbai”, type comma s1.
08:10 Now run the Demo program again.
08:12 We can see that the value of id for s1 has changed to “newid”.
08:19 Let us summarize. In this tutorial we have learnt about
08:24 What is a static method and when it is used
08:28 How to differentiate static methods and instance methods and
08:33 How to create and invoke static methods
08:37 This assignment is a continuation of the Static variable assignment.
08:42 Make sure that you have completed the Static variable assignment.
08:47 We will highlight only the modifications here.
08:50 Here we have a variable to represent “status”.
08:55 It is used to indicate whether the car is “in” for service or “out” after service
09:01 We will also have another variable to represent No of cars out after Service.
09:08 Define a method service( Car c) which updates the status to ”out”.
09:13 Accordingly it modifies the values for
09:17 No of Cars in for Service No of Cars out after Service
09:21 Also define a method show( ) to print all the car details.
09:26 As before, we need to perform the following as listed.
09:30 Note that we have to identify and define the static method as required.
09:35 Also create a Demo class.
09:38 Inside the main method, Create a few objects of CarService.
09:43 Invoke the service( ) method on some of them.
09:47 Invoke the show( ) method using all the objects and verify the results.
09:52 The video at the following link summarizes the Spoken Tutorial Project.
09:57 Please download and watch it.
09:59 The Spoken Tutorial Project team Conducts workshops using spoken tutorials and Gives certificates on passing the online tests
10:08 For more details, please write to us.
10:11 Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.
10:18 More information on this Mission is available at the link shown.
10:22 This script has been contributed by: Dept. of Information Technology, Amal Jyothi College of Engineering.
10:30 This is Priya from IIT Bombay, thanks for joining.

Contributors and Content Editors

Jyotisolanki, Pratik kamble, Sandhya.np14