Java/C3/Static-Variables/English-timed

From Script | Spoken-Tutorial
Revision as of 14:24, 12 August 2016 by PoojaMoolya (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 Variables.
00:05 In this tutorial we will learn about:
  • What are static variables
  • 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:43 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. 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. Let us illustrate the usage of static variables with an example.
01:16 Consider a class created to represent student enrollments in an organization.
01:23 This class contains the Name, Id , Branch and Total Count of students enrolled.
01:30 Now let us visualize what happens when student enrollments are done.
01:36 By default, Total Count is 0. The Name of the first student is ADIL. Id is IT101. Branch is IT.
01:48 Now the Total Count is updated as 1.
01:52 Similarly when the second student AMAL gets enrolled, Total Count is updated to 2.
01:59 When the third student CAROL gets enrolled, Total Count is updated to 3.
02:06 Now we can identify that the variable Total Count is common to all the objects and has a single value.
02:14 So the variable Total Count can be represented as a static variable.
02:20 We can also see that the variables Name, Id and Branch have their own copies for each object.
02:28 Also they have specific values for each object. So these variables can be treated as instance variables.
02:38 Now let us explore the differences between instance variables and static variables.
02:45 Every object has its own copies for instance variables.
02:50 Id, Name, Branch are examples of instance variables.
02:55 Static variables in a class share a single copy with all objects in that class.
03:02 Total Count is an example of static variable.
03:06 When a class is loaded a static variable occupies a single fixed memory location

whereas Instance variables of each object occupy separate memory locations.

03:19 Now let us look at the sample code representing the student enrollment class.
03:25 The name of the class is StudentEnroll. The instance variables are declared as id, name and branch.
03:35 The variable count is declared as static since it is common to the whole class.
03:41 Now we will switch to Eclipse and create a new project called StaticVariableDemo.
03:48 Inside this project we will create the necessary classes to demonstrate the usage of Static variables.
03:56 So right click on src folder and click on new -> class, and type the name of the class as StudentEnroll and Press Enter.
04:07 Now type the code which was shown in the earlier slide.
04:12 Now click on source -> and select Generate Constructor using Fields. Delete the super keyword from the generated code.
04:21 This constructor can initialize the values of the id, name and branch fields.
04:28 We also need to increment the value of the variable count by one every time an object is created.
04:36 So inside the constructor type, count ++ semicolon.
04:42 Now we will add a method showData( ) to this class to print the values of all the variables.
04:50 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.
05:04 Now right click on the default package click on new -> class and then type name as Demo.
05:12 Inside this class we will have the main method.
05:16 So type main and then press ctrl+space to generate the main method.
05:22 Now we need to print the Student Enrollment data.
05:27 We will create a few objects of StudentEnroll class to represent student enrollments.
05:34 So type the following code. StudentEnroll s1 equals new StudentEnroll
05:41 Now we can pass the values of different arguments. Within brackets type IT101 as id, ADIL as name and IT as branch.
05:54 Now let us invoke the showData method to print the enrollment details. So type

s1.showData( ).

06:03 Now run the Demo program.
06:06 We can see that the values of the instance variables corresponding to s1 get printed.
06:13 Also note that the value of the number of student enrollments is 1.
06:19 This is because we have created only 1 object.
06:23 Now type the following code to create one more object s2.
06:28 The showData method can be called once again using s2.
06:33 Again run the Demo Program.
06:36 We can see that the values of the instance variables corresponding to s2 get printed.
06:43 Also note that the value of number of student enrollments is updated to 2 for both s1 and s2.
06:51 Now create one more object s3.
06:54 Now Let us Invoke the showData method again using s3.
07:00 Run the Demo Program again.
07:02 We can see that the values of the instance variables corresponding to s3 get printed.
07:09 Also note that the value of number of student enrollments is now updated to 3 in all the cases.
07:17 Now we can understand that the value of the number of student enrollments is common to all the objects.
07:25 Come back to slides. The static modifier, is also used along with the final modifier.
07:33 It is done to define a constant which is common to the entire class.
07:38 Conventionally, the names of such constant variables are spelled in uppercase letters.
07:44 If the name is composed of more than one word, the words are separated by an underscore.
07:51 As an Example, if all the students are getting enrolled to the same organisation say IITB.
07:59 It can be represented by using a constant static variable say ORG_NAME.
08:06 Usually we declare such constants with a public visibility.
08:11 A Static variable can be accessed directly from another class by using its own class name.

For Example: StudentEnroll.ORG_NAME;

08:24 Now come back to eclipse.
08:27 Open the StudentEnroll class and type the variable declaration as public static final String ORG_NAME = “IITB”;
08:39 Now go to the Demo class and type the following code
08:45 Here you can see that ORG_NAME is accessed by using its class name StudentEnroll.
08:53 Now run the Demo Program again.
08:56 We can see that the ORGANISATION name is getting printed as IITB.
09:01 Let us summarize. In this tutorial we have learnt about
  • What is a static variable and when it is used
  • How to create and invoke the static variables
09:15 As an Assignment. Design a class CarService to represent a Car Service Station.
09:22 This class should contain variables to represent the following details.
09:27 Name of the Service Station. Car make, model and reg. no - which are in for service.

No. of Cars in for Service.

09:37 Identify the instance variables and static variables.
09:41 Declare them using suitable keywords.
09:45 Define a constructor to initialize the values for Car make, model and regno.
09:51 Define a method show( ) to print the values of all the variables.
09:57 Also create a Demo class containing the main method to verify the results.

i.e Create a few objects of CarService.

10:06 Invoke the show( ) method using these objects.
10:10 Also access the static variables directly using the class name.
10:15 The video at the following link summarizes the Spoken Tutorial Project. Please download and watch it.
10:22 The Spoken Tutorial Project Team

• Conducts workshops using spoken tutorials and

• Gives certificates on passing the online tests.

10:32 For more details, please write to us.
10:35 Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.
10:41 More information on this Mission is available at the link shown.
10:46 This script has been contributed by:

Department of Information Technology, Amal Jyothi College of Engineering.

This is Priya from IIT Bombay signing off. Thanks for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14