Difference between revisions of "Java/C3/Static-Variables/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 4: Line 4:
  
 
'''Keywords:''' Static Variables, Class variables, Instance Variables, static keyword, final keyword, static constants, video tutorial
 
'''Keywords:''' Static Variables, Class variables, Instance Variables, static keyword, final keyword, static constants, video tutorial
 +
  
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
| style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <center>'''Visual Cue'''</center>
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <center>'''Visual Cue'''</center>
| style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <center>'''Narration'''</center>
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <center>'''Narration'''</center>
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 1'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 1'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Welcome to the Spoken Tutorial on '''Static Variables.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Welcome to the Spoken Tutorial on '''Static Variables.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 2'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 2'''
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we will learn about:
+
* What are static variables
 +
* Creating''' '''static variables
 +
* How to''' '''use''' '''static variables
 +
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we will learn about:
  
 
* What are''' static variables'''
 
* What are''' static variables'''
 
* Creating''' static variables '''and
 
* Creating''' static variables '''and
 
* How to''' '''use''' static variables'''
 
* How to''' '''use''' static variables'''
 +
 +
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 3'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 3'''
  
 
'''Software Requirements'''
 
'''Software Requirements'''
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here we are using
+
* Ubuntu 12.04
 +
* JDK 1.7
 +
* Eclipse 4.3.1
 +
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here we are using
  
 
* '''Ubuntu 12.04'''
 
* '''Ubuntu 12.04'''
 
* '''JDK 1.7 '''and
 
* '''JDK 1.7 '''and
 
* '''Eclipse 4.3.1'''
 
* '''Eclipse 4.3.1'''
 +
 +
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 4'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 4'''
  
 
'''Prerequisites'''
 
'''Prerequisites'''
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial, you must have knowledge of basics of''' Java''' and''' Eclipse IDE.'''
+
* Basic knowledge of '''Java''' and '''Eclipse IDE'''
 +
* Knowledge of classes, objects and instance variables
 +
* For relevant tutorials, please visit http://www.spoken-tutorial.org
 +
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial, you must have knowledge of basics of''' Java''' and''' Eclipse IDE.'''
  
 
You should also have the knowledge of''' classes, objects '''and''' instance variables''' in''' Java.'''
 
You should also have the knowledge of''' classes, objects '''and''' instance variables''' in''' Java.'''
Line 48: Line 68:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 5'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 5'''
  
 
'''What is a static variable?'''
 
'''What is a static variable?'''
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A''' static variable''' is a variable which is associated with the entire''' class.'''
+
* A '''static variable''' is a variable which is associated with the entire '''class'''
 +
* It is also called a '''class variable'''
 +
* It is declared using the '''static''' keyword
 +
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A''' static variable''' is a variable which is associated with the entire''' class.'''
 +
 
 +
 
 +
It is also called a''' class variable.'''
  
It is also called a '''class variable.'''
 
  
 
It is declared using the''' static keyword.'''
 
It is declared using the''' static keyword.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 6'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We have seen about static variable briefly in the earlier tutorial.
  
'''Static Variable-Example'''
 
 
(Proceed with the Slide)
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We have seen about static variable briefly in the earlier tutorial.
 
  
 
In this tutorial we will see it in detail.
 
In this tutorial we will see it in detail.
  
Let us illustrate the usage of''' static variables''' with an example.
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In Eclipse IDE, create a class StaticVariableDemo
 +
 
 +
 
 +
 
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will switch to''' Eclipse''' and create a new project called '''StaticVariableDemo'''.
 +
 
 +
 
 +
Inside this project we will create the necessary classes to demonstrate the usage of''' Static variables.'''
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on '''src''' folder -> '''new '''-> '''class''', type '''StudentEnroll''' and hit Enter
 +
 
 +
 
 +
Go back to slide 8 and show the code
 +
 
 +
Copy and paste the code
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| So right click on '''src''' folder and click '''new '''-> '''class''', and type the name of the class as''' StudentEnroll''' and press '''Enter'''.
 +
 
 +
 
 +
 
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us illustrate the usage of''' static variables''' with an example.
 +
 
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 6'''
 +
 
 +
'''Static Variable-Example'''
 +
 
 +
 
 +
(Proceed with the Slide)
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Consider a''' class''' created to represent student enrollments in an organisation.
  
Consider a''' class''' created to represent student enrollments in an organization.
 
  
 
This class contains the '''Name, Id , Branch''' and''' Total Count''' of students enrolled.
 
This class contains the '''Name, Id , Branch''' and''' Total Count''' of students enrolled.
 +
  
 
Now let us visualize what happens when student enrollments are done.
 
Now let us visualize what happens when student enrollments are done.
  
By default, '''Total Count''' is 0.
 
  
The''' Name''' of the first student is '''ADIL.'''
+
By default '''Total Count''' is 0.
  
'''Id''' is''' IT101. Branch''' is''' IT.'''
 
  
Now the''' Total Count''' is updated as 1.
+
The''' Name''' of the first student is '''ADIL.'''
  
Similarly when the second student''' AMAL '''gets enrolled, '''Total Count''' is updated to 2.
 
  
When the third student''' CAROL''' gets enrolled,''' Total Count''' is updated to 3.
+
'''Id''' is''' IT101. Branch''' is''' IT.'''
  
Now we can identify that the variable''' Total Count''' is common to all the objects and has a single value.
 
  
So the variable''' Total Count''' can be represented as a''' static variable'''.
+
Now the''' Total Count''' is updated as 1.
  
We can also see that the variables''' Name, Id''' and''' Branch''' have their own copies for each object.
 
  
Also they have specific values for each object.
+
Similarly when the second student''' AMAL '''gets enrolled '''Total Count''' is updated to 2.
  
So these variables can be treated as''' instance variables.'''
 
  
|-
+
When the third student''' CAROL''' gets enrolled,''' Total Count''' is updated to 3.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 7'''
+
  
'''Instance Vs Static Variable'''
 
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us explore the differences between''' instance variables''' and''' static variables.'''
+
Now we can identify that the variable''' Total Count''' is common to all the objects and has a single value.
  
Every object has its own copies for '''instance variables.'''
 
  
'''Id, Name, Branch''' are examples of instance variables.
+
So the variable''' Total Count''' can be represented as a''' static variable'''.
  
'''Static variables''' in a class share a single
 
  
copy with all objects in that class.
+
We can also see that the variables''' Name, Id''' and''' Branch''' have their own copies for each object.
  
'''Total Count''' is an example of static variable.
 
  
|-
+
Also they have specific values for each object.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 7(A)'''
+
  
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| When a class is loaded
+
So these variables can be treated as''' instance variables'''
  
A''' static variable''' occupies a single fixed memory location
 
  
whereas
 
  
'''Instance variables''' of each object occupy separate''' '''memory locations.
 
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 8'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| public class StudentEnroll {
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us look at the sample code representing the student enrollment class.
+
private String id, name, branch;
 +
 
 +
public static int count=0;
 +
 
 +
'''}'''
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us look at the code representing the '''student enrollment class'''.
  
 
The name of the class is''' StudentEnroll.'''
 
The name of the class is''' StudentEnroll.'''
Line 139: Line 186:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In Eclipse IDE, create a class StaticVariableDemo
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 7(A)'''
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will switch to''' Eclipse''' and create a new project called '''StaticVariableDemo'''.
+
When a class is loaded
  
Inside this project we will create the necessary classes to demonstrate the usage of''' Static variables.'''
+
* A '''static variable''' in a '''class''' occupies a single fixed memory location
 +
* '''Instance variables''' of each '''object''' occupy separate memory locations
  
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on '''src''' folder -> '''new '''-> '''class''', type '''StudentEnroll''' and hit Enter
 
  
Go back to slide 8 and show the code
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| When a class is loaded
  
Copy and paste the code
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| So right click on '''src''' folder and click on '''new '''-> '''class''', and type the name of the class as''' StudentEnroll''' and Press '''Enter'''.
 
  
Now type the code which was shown in the earlier slide.
+
A''' static variable''' occupies a single fixed memory location
 +
 
 +
 
 +
whereas
 +
 
 +
 
 +
'''Instance variables''' of each object occupy separate''' '''memory locations.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| click on''' source ->''' and select''' Generate Constructor using Fields'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| click on''' source ->''' and select''' Generate Constructor using Fields'''
 +
 
  
 
Delete the''' super''' keyword
 
Delete the''' super''' keyword
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now click on''' source ->''' and select''' Generate Constructor using Fields.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now click on''' source ->''' and select''' Generate Constructor using Fields,'''
 +
 
  
 
Delete the''' super''' keyword from the generated code.
 
Delete the''' super''' keyword from the generated code.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Constructor''' code
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Constructor''' code
 +
 
  
 
public StudentEnroll(String id, String name, String branch) {
 
public StudentEnroll(String id, String name, String branch) {
Line 178: Line 231:
 
}
 
}
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This '''constructor''' can initialize the values of the''' id, name''' and''' branch '''fields.
+
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This '''constructor''' can initialise the values of the''' id, name''' and''' branch '''fields.
 +
 
  
 
We also need to increment the value of the variable''' count''' by one every time an object is created.
 
We also need to increment the value of the variable''' count''' by one every time an object is created.
Line 184: Line 240:
 
So inside the constructor type,
 
So inside the constructor type,
  
'''count ++ semicolon.'''
+
'''count ++ semicolon'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type, public void showData()
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type, public void showData()
  
 
{// Copy the code below   
 
{// Copy the code below   
Line 194: Line 250:
  
 
}  
 
}  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will add a''' '''method '''showData'''( ) to this class to print the values of all the variables.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will add a''' '''method '''showData'''( ) to this class to print the the values of all the variables.
  
 
So type''' public void showData( )'''  
 
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'''.
+
within brackets type the following code to print the values of''' id, name, branch,''' total number of students enrolled.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on the '''default package,''' click new-> class and then type name as''' Demo'''.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on the '''default package,''' click new-> class and then type name as''' Demo'''.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now right click on the '''default package''' click on '''new '''-> '''class''' and then type name as''' Demo.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now right click on the '''default package''' click '''new '''-> '''class''' and then type name as''' Demo.'''
 +
 
  
 
Inside this class we will have the '''main''' method.
 
Inside this class we will have the '''main''' method.
 +
  
 
So type '''main''' and then press ctrl+space to generate the''' main method.'''
 
So type '''main''' and then press ctrl+space to generate the''' main method.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| TypeSystem.out.println("Student Enrollment Data");
 +
 
  
 
StudentEnroll s1=new StudentEnroll("IT101","ADIL","IT");
 
StudentEnroll s1=new StudentEnroll("IT101","ADIL","IT");
 +
  
 
Type s1.showData( );
 
Type s1.showData( );
  
Click on '''Run '''icon
 
  
 
Highlight the values of s1
 
Highlight the values of s1
 +
  
 
Highlight '''1'''
 
Highlight '''1'''
 +
  
 
StudentEnroll s2=new StudentEnroll("EC101","AMAL","EC");
 
StudentEnroll s2=new StudentEnroll("EC101","AMAL","EC");
 +
  
 
copy and paste, s2.showData();
 
copy and paste, s2.showData();
  
Click on '''Run '''icon
 
  
Highlight values
+
Click on '''Run''' icon
 +
 
  
 
Highlight '''2'''
 
Highlight '''2'''
 +
  
 
StudentEnroll s3=new StudentEnroll("CS101","CAROL","CS");
 
StudentEnroll s3=new StudentEnroll("CS101","CAROL","CS");
 +
  
 
Type, s3.showData();
 
Type, s3.showData();
  
Click on '''Run '''icon
 
  
Hightlight the values
+
Click on '''Run''' icon
 +
 
  
 
Highlight '''3'''
 
Highlight '''3'''
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we need to print the Student Enrollment data.
+
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we need to print the '''Student Enrollment data'''.
 +
 
  
 
We will create a few objects of''' StudentEnroll class''' to represent student enrollments.
 
We will create a few objects of''' StudentEnroll class''' to represent student enrollments.
  
So type''' '''the following code.
 
  
'''StudentEnroll s1 equals new StudentEnroll'''
+
So type''' StudentEnroll s1 equals new StudentEnroll'''
 +
 
 +
 
 +
Now we can pass the values of different arguments
  
Now we can pass the values of different arguments.
 
  
 
Within brackets type''' IT101 '''as''' id, ADIL''' as''' name '''and''' IT''' as '''branch.'''
 
Within brackets type''' IT101 '''as''' id, ADIL''' as''' name '''and''' IT''' as '''branch.'''
 +
  
 
Now let us invoke the''' showData''' method to print the enrollment details. So type
 
Now let us invoke the''' showData''' method to print the enrollment details. So type
  
'''s1.showData( )'''.
 
  
Now run the '''Demo''' program.
+
'''s1.showData( )'''
 +
 
 +
 
 +
Now run the '''Demo''' program
 +
 
  
 
We can see that the values of the instance variables corresponding to''' s1''' get printed.
 
We can see that the values of the instance variables corresponding to''' s1''' get printed.
  
Also note that the value of the number of student enrollments is 1.
+
 
 +
Also note that the value of the number of student enrollments is 1
 +
 
  
 
This is because we have created only 1 object.
 
This is because we have created only 1 object.
 +
  
 
Now type the following code to create one more object''' s2.'''
 
Now type the following code to create one more object''' s2.'''
 +
  
 
The''' showData''' method can be called once again using''' s2.'''
 
The''' showData''' method can be called once again using''' s2.'''
 +
  
 
Again run the '''Demo''' Program.
 
Again run the '''Demo''' Program.
 +
  
 
We can see that the values of the instance variables corresponding to''' s2''' get printed.
 
We can see that the values of the instance variables corresponding to''' s2''' get printed.
 +
  
 
Also note that the value of''' number of student''' enrollments is updated to 2 for both''' s1''' and''' s2.'''
 
Also note that the value of''' number of student''' enrollments is updated to 2 for both''' s1''' and''' s2.'''
 +
  
 
Now create one more object''' s3.'''
 
Now create one more object''' s3.'''
 +
  
 
Now Let us Invoke the''' showData''' method again using''' s3.'''
 
Now Let us Invoke the''' showData''' method again using''' s3.'''
 +
  
 
Run the '''Demo''' Program again.
 
Run the '''Demo''' Program again.
  
We can see that the values of the instance variables corresponding to''' s3''' get printed.
 
  
Also note that the value of''' '''number of student enrollments is now updated to 3 in all the cases.
+
We can see that the values of instance variables corresponding to''' s3''' get printed.
  
Now we can understand that the value of the number of student enrollments is common to all the objects.
 
  
|-
+
Also note that the value of''' '''number of student enrollments is now updated to 3 in all the cases.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 9'''
+
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Come back to slides.
 
  
The''' static''' modifier, is also used along with the final modifier.
+
Now we can understand that value of number of student enrollments is common to all the objects.
 
+
It is done to define a''' constant''' which is common to the entire class.
+
 
+
Conventionally, the names of such constant variables are spelled in uppercase letters.
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 9(A)'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 9'''
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| If the name is composed of more than one word, the words are separated by '''an underscore.'''
+
'''Static Variables - As Constants'''
  
As an Example, if all the students are getting enrolled to the same organisation say''' IITB'''.
+
* The '''static '''modifier is also used along with the''' final '''modifier
 +
* It is done to define a '''constant''' which is common to the entire class
 +
* The names of constant variables are spelled in upper-case letters
  
It can be represented by using a constant static variable say''' ORG_NAME'''.
 
  
Usually we declare such constants
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Come back to slides.
  
with a '''public''' visibility.
 
  
|-
+
The''' static''' modifier, is also used along with the final modifier.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 10'''
+
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A Static variable can be accessed
 
  
directly from another class by using its own
+
It is done to define a''' constant''' which is common to the entire class.
  
class name.
 
  
For Example:
+
 
  
StudentEnroll.ORG_NAME;
+
Conventionally, the names of such constant variables are spelled in uppercase letters.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Come back to eclipse
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Come back to eclipse
  
  
Line 331: Line 402:
  
 
'''public static final String ORG_NAME = “IITB”;'''
 
'''public static final String ORG_NAME = “IITB”;'''
 +
  
 
Inside the '''Demo''' class, type '''System.out.println(“ORGANISATION:”+StudentEnroll.ORG_NAME);'''
 
Inside the '''Demo''' class, type '''System.out.println(“ORGANISATION:”+StudentEnroll.ORG_NAME);'''
Line 339: Line 411:
  
 
Point to '''ORGANISATION: IITB'''
 
Point to '''ORGANISATION: IITB'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now come back to eclipse.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now come back to eclipse.
 +
 
  
 
Open the '''StudentEnroll''' class and type the variable declaration as  
 
Open the '''StudentEnroll''' class and type the variable declaration as  
 +
  
 
'''public static final String ORG_NAME = “IITB”;'''
 
'''public static final String ORG_NAME = “IITB”;'''
+
 
Now go to the '''Demo class '''and type the following code
+
 
 +
As an example, if all the students are getting enrolled to the same organisation say''' IITB'''.
 +
 
 +
 
 +
It can be represented by using a constant static variable say''' ORG_NAME.'''
 +
 
 +
 
 +
If the name is composed of more than one word, the words are separated by an''' underscore.'''
 +
 
 +
 
 +
Usually we declare such constants
 +
 
 +
with a '''public '''visibility.
 +
 
 +
 
 +
 
 +
 
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Click on '''Run''' icon
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now go to the '''Demo class '''and type the following code
 +
 
  
 
Here you can see that '''ORG_NAME '''is accessed by using its class name '''StudentEnroll.'''
 
Here you can see that '''ORG_NAME '''is accessed by using its class name '''StudentEnroll.'''
  
'''  '''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Click on '''Run''' icon
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''  '''
  
 
Now run the '''Demo''' Program again.
 
Now run the '''Demo''' Program again.
Line 356: Line 452:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 10'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 10'''
  
 
Summary
 
Summary
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us summarize.
+
* What is a static variable and when it is used
 +
* How to create and invoke the static variables
 +
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us summarize.
  
 
In this tutorial we have learnt about
 
In this tutorial we have learnt about
Line 366: Line 466:
 
* What is a static variable and when it is used
 
* What is a static variable and when it is used
 
* How to create and invoke the static variables
 
* How to create and invoke the static variables
 +
 +
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 11'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 11'''
  
 
Assignment
 
Assignment
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an Assignment.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an Assignment
 +
 
  
 
Design a class''' CarService''' to represent a Car Service Station.
 
Design a class''' CarService''' to represent a Car Service Station.
  
This class should contain variables to represent the following details.
 
  
'''Name''' of the Service Station.
+
This class should contain variables to represent the following details:
  
Car '''make''', '''model''' and '''reg. no''' - which are in for service.
 
  
'''No. of Cars in''' for Service.
+
'''Name''' of the Service Station
  
Identify the instance variables and static variables.
 
  
Declare them using suitable keywords.
+
Car '''make''', '''model''' and '''reg. no''' - which are in for service
  
Define a''' constructor''' to initialize the values for Car make, model and regno.
 
  
Define a method''' show( )''' to print the values of all the variables.
+
'''No. of Cars in''' for Service
 +
 
 +
 
 +
Identify the instance variables and static variables
 +
 
 +
 
 +
Declare them using suitable keywords
 +
 
 +
 
 +
Define a''' constructor''' to initialise the values for Car '''make, model''' and '''regno'''
 +
 
 +
 
 +
Define a method''' show( )''' to print the values of all the variables
 +
 
 +
 
 +
Also create a''' Demo''' class containing the main method to verify the results
  
Also create a''' Demo''' class containing the main method to verify the results.
 
  
 
i.e
 
i.e
  
Create a few objects of''' CarService.'''
 
  
Invoke the''' show( ) '''method using these objects.
+
Create a few objects of''' CarService'''
 +
 
 +
 
 +
Invoke the''' show( ) '''method using these objects
 +
 
  
 
Also access the static variables directly using the class name.
 
Also access the static variables directly using the class name.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Project'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Project'''
 +
 
 +
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The video at the following link summarizes the Spoken Tutorial Project.
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The video at the following link summarizes the Spoken Tutorial Project.
 
  
 
Please download and watch it.
 
Please download and watch it.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Workshops'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Workshops'''
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The Spoken Tutorial Project Team
+
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The Spoken Tutorial Project Team
  
 
• Conducts workshops using spoken tutorials and
 
• Conducts workshops using spoken tutorials and
  
• Gives certificates on passing the online tests.
+
• Gives certificates on passing the online tests
 +
 
  
 
For more details, please write to us.
 
For more details, please write to us.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About NMEICT'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About NMEICT'''
 +
 
 +
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.
 
  
 
More information on this Mission is available at the link shown.
 
More information on this Mission is available at the link shown.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Contributor slide'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Contributor slide'''
 +
 
 +
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This script has been contributed by:
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This script has been contributed by:
+
Department of Information Technology, Amal Jyothi College of Engineering
  
Department of Information Technology, Amal Jyothi College of Engineering.
 
  
 
This is Priya from IIT Bombay signing off. Thanks for joining.
 
This is Priya from IIT Bombay signing off. Thanks for joining.
  
 
|}
 
|}

Revision as of 12:06, 12 January 2017

Title of script: Static Variables

Author: Joms Antony

Keywords: Static Variables, Class variables, Instance Variables, static keyword, final keyword, static constants, video tutorial


Visual Cue
Narration
Slide 1 Welcome to the Spoken Tutorial on Static Variables.
Slide 2

Learning Objectives

  • What are static variables
  • Creating static variables
  • How to use static variables


In this tutorial we will learn about:
  • What are static variables
  • Creating static variables and
  • How to use static variables


Slide 3

Software Requirements

  • Ubuntu 12.04
  • JDK 1.7
  • Eclipse 4.3.1


Here we are using
  • Ubuntu 12.04
  • JDK 1.7 and
  • Eclipse 4.3.1


Slide 4

Prerequisites

  • Basic knowledge of Java and Eclipse IDE
  • Knowledge of classes, objects and instance variables
  • For relevant tutorials, please visit http://www.spoken-tutorial.org


To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.

You should also have the knowledge of classes, objects and instance variables in Java.

If not, for relevant Java tutorials, please visit the link shown.

Slide 5

What is a static variable?

  • A static variable is a variable which is associated with the entire class
  • It is also called a class variable
  • It is declared using the static keyword


A static variable is a variable which is associated with the entire class.


It is also called a class variable.


It is declared using the static keyword.

We have seen about static variable briefly in the earlier tutorial.


In this tutorial we will see it in detail.

In Eclipse IDE, create a class StaticVariableDemo


Now we will switch to Eclipse and create a new project called StaticVariableDemo.


Inside this project we will create the necessary classes to demonstrate the usage of Static variables.

Right click on src folder -> new -> class, type StudentEnroll and hit Enter


Go back to slide 8 and show the code

Copy and paste the code

So right click on src folder and click new -> class, and type the name of the class as StudentEnroll and press Enter.



Let us illustrate the usage of static variables with an example.
Slide 6

Static Variable-Example


(Proceed with the Slide)

Consider a class created to represent student enrollments in an organisation.


This class contains the Name, Id , Branch and Total Count of students enrolled.


Now let us visualize what happens when student enrollments are done.


By default Total Count is 0.


The Name of the first student is ADIL.


Id is IT101. Branch is IT.


Now the Total Count is updated as 1.


Similarly when the second student AMAL gets enrolled Total Count is updated to 2.


When the third student CAROL gets enrolled, Total Count is updated to 3.


Now we can identify that the variable Total Count is common to all the objects and has a single value.


So the variable Total Count can be represented as a static variable.


We can also see that the variables Name, Id and Branch have their own copies for each object.


Also they have specific values for each object.


So these variables can be treated as instance variables



public class StudentEnroll {

private String id, name, branch;

public static int count=0;

}

Now let us look at the code representing the student enrollment class.

The name of the class is StudentEnroll.

The instance variables are declared as id, name and branch.

The variable count is declared as static since it is common to the whole class.

Slide 7(A)

When a class is loaded

  • A static variable in a class occupies a single fixed memory location
  • Instance variables of each object occupy separate memory locations


When a class is loaded


A static variable occupies a single fixed memory location


whereas


Instance variables of each object occupy separate memory locations.

click on source -> and select Generate Constructor using Fields


Delete the super keyword

Now click on source -> and select Generate Constructor using Fields,


Delete the super keyword from the generated code.

Constructor code


public StudentEnroll(String id, String name, String branch) {

this.id = id;

this.name = name;

this.branch = branch;

count++;

}


This constructor can initialise the values of the id, name and branch fields.


We also need to increment the value of the variable count by one every time an object is created.

So inside the constructor type,

count ++ semicolon

Type, public void showData()

{// Copy the code below

System.out.println(id+" " +name+" "+branch+" "+count);

}

Now we will add a method showData( ) to this class to print the the values of all the variables.

So type public void showData( )

within brackets type the following code to print the values of id, name, branch, total number of students enrolled.

Right click on the default package, click new-> class and then type name as Demo. Now right click on the default package click new -> class and then type name as Demo.


Inside this class we will have the main method.


So type main and then press ctrl+space to generate the main method.

TypeSystem.out.println("Student Enrollment Data");


StudentEnroll s1=new StudentEnroll("IT101","ADIL","IT");


Type s1.showData( );


Highlight the values of s1


Highlight 1


StudentEnroll s2=new StudentEnroll("EC101","AMAL","EC");


copy and paste, s2.showData();


Click on Run icon


Highlight 2


StudentEnroll s3=new StudentEnroll("CS101","CAROL","CS");


Type, s3.showData();


Click on Run icon


Highlight 3


Now we need to print the Student Enrollment data.


We will create a few objects of StudentEnroll class to represent student enrollments.


So type StudentEnroll s1 equals new StudentEnroll


Now we can pass the values of different arguments


Within brackets type IT101 as id, ADIL as name and IT as branch.


Now let us invoke the showData method to print the enrollment details. So type


s1.showData( )


Now run the Demo program


We can see that the values of the instance variables corresponding to s1 get printed.


Also note that the value of the number of student enrollments is 1


This is because we have created only 1 object.


Now type the following code to create one more object s2.


The showData method can be called once again using s2.


Again run the Demo Program.


We can see that the values of the instance variables corresponding to s2 get printed.


Also note that the value of number of student enrollments is updated to 2 for both s1 and s2.


Now create one more object s3.


Now Let us Invoke the showData method again using s3.


Run the Demo Program again.


We can see that the values of instance variables corresponding to s3 get printed.


Also note that the value of number of student enrollments is now updated to 3 in all the cases.


Now we can understand that value of number of student enrollments is common to all the objects.

Slide 9

Static Variables - As Constants

  • The static modifier is also used along with the final modifier
  • It is done to define a constant which is common to the entire class
  • The names of constant variables are spelled in upper-case letters


Come back to slides.


The static modifier, is also used along with the final modifier.


It is done to define a constant which is common to the entire class.



Conventionally, the names of such constant variables are spelled in uppercase letters.

Come back to eclipse


In StudentEnroll class and type,

public static final String ORG_NAME = “IITB”;


Inside the Demo class, type System.out.println(“ORGANISATION:”+StudentEnroll.ORG_NAME);

Highlight ORG_NAME and StudentEnroll

Run the Demo program

Point to ORGANISATION: IITB

Now come back to eclipse.


Open the StudentEnroll class and type the variable declaration as


public static final String ORG_NAME = “IITB”;


As an example, if all the students are getting enrolled to the same organisation say IITB.


It can be represented by using a constant static variable say ORG_NAME.


If the name is composed of more than one word, the words are separated by an underscore.


Usually we declare such constants

with a public visibility.



Click on Run icon Now go to the Demo class and type the following code


Here you can see that ORG_NAME is accessed by using its class name StudentEnroll.

Click on Run icon

Now run the Demo Program again.

We can see that the ORGANISATION name is getting printed as IITB.

Slide 10

Summary

  • What is a static variable and when it is used
  • How to create and invoke the static variables


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


Slide 11

Assignment

As an Assignment


Design a class CarService to represent a Car Service Station.


This class should contain variables to represent the following details:


Name of the Service Station


Car make, model and reg. no - which are in for service


No. of Cars in for Service


Identify the instance variables and static variables


Declare them using suitable keywords


Define a constructor to initialise the values for Car make, model and regno


Define a method show( ) to print the values of all the variables


Also create a Demo class containing the main method to verify the results


i.e


Create a few objects of CarService


Invoke the show( ) method using these objects


Also access the static variables directly using the class name.

About Project


The video at the following link summarizes the Spoken Tutorial Project.


Please download and watch it.

About Workshops


The Spoken Tutorial Project Team

• Conducts workshops using spoken tutorials and

• Gives certificates on passing the online tests


For more details, please write to us.

About NMEICT


Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.


More information on this Mission is available at the link shown.

Contributor slide


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

Nancyvarkey, Nirmala Venkat, Priyacst