Java/C3/Static-Variables/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time
Narration
00:01 Welcome to the Spoken Tutorial on Static Variables.
00:05 In this tutorial, we will learn about: What are static variables,
00:10 creating static variables and how to use static variables.
00:17 Here, we are using:

Ubuntu 12.04,

JDK 1.7 and

Eclipse 4.3.1

00:27 To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.
00:35 You should also have the knowledge of classes, objects and instance variables in Java.
00:42 If not, for relevant Java tutorials, please visit the link shown.
00:49 A static variable is a variable which is associated with the entire class.
00:55 It is also called a class variable.
00:58 It is declared using the static keyword.
01:02 We have seen about static variable briefly in the earlier tutorial.
01:08 In this tutorial, we will see it in detail.
01:11 Now, we will switch to Eclipse and create a new project called StaticVariableDemo.
01:18 Inside this project, we will create the necessary classes to demonstrate the usage of Static variables.
01:26 So, right-click on src folder and click New > Class and type the name of the class as StudentEnroll and press Enter.
01:37 Let us illustrate the usage of static variables with an example.
01:42 Consider a class created to represent student enrollments in an organisation.
01:49 This class contains the Name, Id , Branch and Total Count of students enrolled.
01:56 Now, let us visualize what happens when student enrollments are done.
02:02 By default, Total Count is 0. The Name of the first student is ADIL.
02:09 Id is IT101, Branch is IT.
02:14 Now the Total Count is updated as 1.
02:18 Similarly when the second student AMAL gets enrolled, Total Count is updated to 2.
02:25 When the third student CAROL gets enrolled, Total Count is updated to 3.
02:32 Now we can identify that the variable Total Count is common to all the objects and has a single value.
02:40 So, the variable Total Count can be represented as a static variable.
02:45 We can also see that the variables Name, Id and Branch have their own copies for each object.
02:54 Also they have specific values for each object.
02:59 So, these variables can be treated as instance variables.
03:04 Now, let us look at the code representing the student Enroll class.
03:09 The instance variables are declared as id, name and branch.
03:16 The variable count is declared as static since it is common to the whole class.
03:22 When a class is loaded, a static variable occupies a single fixed memory location.
03:28 Whereas Instance variables of each object occupy separate memory locations.
03:35 Now click on Source > and select Generate Constructor using Fields.
03:41 Delete the super keyword from the generated code.
03:45 This constructor can initialise the values of the id, name and branch fields.
03:51 We also need to increment the value of the variable count by one every time an object is created.
03:59 So, inside the constructor, type: count ++ semicolon.
04:05 Now we will add a method showData( ) to this class, to print the values of all the variables.
04:13 So, type public void showData( ) within brackets type the following code to print the values of id, name, branch, and total number of students enrolled.
04:27 Now right-click on the default package and click on New > Class and then type name as Demo.
04:36 Inside this class, we will have the main method.
04:39 So, type main and then press ctrl+space to generate the main method.
04:46 Now we need to print the Student Enrollment data.
04:50 We will create a few objects of StudentEnroll class to represent student enrollments.
04:57 So, type the following code: StudentEnroll s1 equals new StudentEnroll.
05:04 Now we can pass the values of different arguments.
05:08 Within brackets, type IT101 as id, ADIL as name and IT as branch.
05:17 Now let us invoke the showData method to print the enrollment details.
05:22 So, type: s1.showData( ). Now run the Demo program.
05:29 We can see that the values of the instance variables corresponding to s1 get printed.
05:36 Also note that the value of the number of student enrollments is 1.
05:42 This is because we have created only 1 object.
05:47 Now type the following code to create one more object s2.
05:52 The showData method can be called once again using s2.
05:56 Again run the Demo Program.
05:59 We can see that the values of the instance variables corresponding to s2 get printed.
06:06 Also, note that the value of number of student enrollments is updated to 2 for both s1 and s2.
06:14 Now create one more object s3.
06:18 Now let us Invoke the showData method again using s3.
06:23 Run the Demo program again.
06:26 We can see that the values of instance variables corresponding to s3 get printed.
06:32 Also note that the value of number of student enrollments is now updated to 3 in all the cases.
06:41 Now we can understand that value of number of student enrollments is common to all the objects.
06:48 Come back to slides.
06:51 The static modifier is also used along with the final modifier.
06:56 It is done to define a constant which is common to the entire class.
07:01 Conventionally, the names of such constant variables are spelt in uppercase letters.
07:08 Now come back to eclipse.
07:11 Open the StudentEnroll class and type the variable declaration as public static final String ORG_NAME = “IITB”;
07:23 As an example, if all the students are getting enrolled to the same organisation, say IITB.
07:31 It can be represented by using a constant static variable say ORG_NAME.
07:38 If the name is composed of more than one word, the words are separated by an underscore.
07:44 Usually we declare such constants with a public visibility.
07:49 Now go to the Demo class and type the following code.
07:55 Here you can see that ORG_NAME is accessed by using its class name StudentEnroll.
08:03 Now run the Demo program again.
08:06 We can see that the ORGANISATION name is getting printed as IITB.
08:11 Let us summarize. In this tutorial, we have learnt about:
08:17 What is a static variable and when it is used
08:21 How to create and invoke the static variables.
08:25 As an Assignment, design a class CarService to represent a car service station.
08:32 This class should contain variables to represent the following details: Name of the Service Station,
08:39 Car make, model and register number - which are in for service
08:44 No. of Cars in for Service.
08:47 Identify the instance variables and static variables.
08:51 Declare them using suitable keywords.
08:55 Define a constructor to initialise the values for Car make, model and register number.
09:01 Define a method show( ) to print the values of all the variables.
09:07 Also, create a Demo class containing the main method to verify the results

i.e create a few objects of CarService.

09:16 Invoke the show( ) method using these objects.
09:20 Also, access the static variables directly using the class name.
09:25 The video at the following link summarizes the Spoken Tutorial Project. Please download and watch it.
09:32 The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials and

Gives certificates on passing the online tests.

09:41 For more details, please write to us.
09:45 Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.
09:51 More information on this mission is available at the link shown.
09:56 This script has been contributed by: Department of Information Technology, Amal Jyothi College of Engineering.
10:03 This is Priya from IIT Bombay, signing off. Thanks for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14