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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script:''' Static Methods '''Author:''' Joms Antony '''Keywords:''' Static methods, Class methods, Instance methods, Static variables {| style="border-spacing:...")
 
 
(One intermediate revision by one other user not shown)
Line 23: Line 23:
 
* Difference between''' instance methods''' and''' static methods'''
 
* Difference between''' instance methods''' and''' static methods'''
 
* How to use''' static methods'''
 
* How to use''' static methods'''
 
 
 
| 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:
 
| 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:
  
Line 31: Line 29:
 
* Difference between''' instance methods''' and''' static methods '''and
 
* Difference between''' instance methods''' and''' static methods '''and
 
* How to use''' static methods'''
 
* How to use''' static methods'''
 
 
  
 
|-
 
|-
Line 42: Line 38:
 
* '''JDK 1 .7'''
 
* '''JDK 1 .7'''
 
* '''Eclipse 4.3.1'''
 
* '''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
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here we are using
  
Line 49: Line 43:
 
* '''JDK 1 .7 '''and
 
* '''JDK 1 .7 '''and
 
* '''Eclipse 4.3.1'''
 
* '''Eclipse 4.3.1'''
 
 
  
 
|-
 
|-
Line 60: Line 52:
 
* Knowledge of''' instance variables, methods''' and''' static variables '''in''' Java'''
 
* Knowledge of''' instance variables, methods''' and''' static variables '''in''' Java'''
 
* For relevant tutorials, please visit http://www.spoken-tutorial.org
 
* 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,  
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial,  
  
Line 76: Line 66:
  
  
* A''' static method''' is a method which is associated with the entire''' class'''
+
* A''' static method''' is a '''method''' which is associated with the entire''' class'''
  
 
* It is also called a''' class method''' and is declared using the''' static keyword.'''
 
* It is also called a''' class method''' and is declared using the''' static keyword.'''
Line 83: Line 73:
  
  
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A''' static method''' is a method which is associated with the entire''' class.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A''' static method''' is a '''method''' which is associated with the entire''' class.'''
  
  
Line 92: Line 82:
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 6'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In Eclipse IDE, create a project '''StaticMethodDemo'''
  
'''Static Method – Example'''
 
  
  
'''Show the final shot in the slide.'''
+
| style="border: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''' StaticMethodDemo'''.
  
'''(Three students enrolled and total count as 3)'''
 
  
 
+
Inside this project we will create the necessary '''classes''' to demonstrate the usage of''' Static methods.'''
Highlight '''name''' and '''id'''
+
 
+
 
+
Highlight '''organization''' and''' total count'''
+
 
+
 
+
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us see how to use''' static methods''' with an example'''.'''
+
 
+
 
+
This example is very similar to the one which is used in the '''Static Variable''' tutorial.
+
 
+
 
+
Here again we are representing the''' StudentEnroll''' class.
+
 
+
 
+
Recall that the variables''' name''' and''' id''' are handled as''' instance variables.'''
+
 
+
 
+
Here the variables''' organization''' and''' total count''' are common to the entire class.
+
 
+
 
+
So they can be treated as '''static variables'''.
+
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 7'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on''' src''' folder and click '''New'''->''' Class '''and type the class name as''' StudentEnroll''' and hit '''Enter'''
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We will create a new''' class StudentEnroll.'''
'''Static Method'''
+
 
+
* Any method which is used to handle''' static variables''' can be defined as a''' static method'''
+
* If a''' method''' is used to change the value of''' Organisation name,''' then it can be defined as a''' static method'''
+
 
+
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Any method which is used to handle''' static variables''' can be defined as a''' static method.'''
+
 
+
 
+
Say for example, if a''' method''' is used to change the value of''' Organisation name,''' then it can be defined as a''' static method.'''
+
 
+
 
+
 
+
  
 
|-
 
|-
| style="border:1pt solid #000001;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;"| '''Slide 6'''
  
'''Instance Vs Static Methods'''
+
'''Static Method – Example'''
  
* '''Instance methods''' can access''' static variables'''
 
* A '''Static method''' can directly access
 
  
and modify only''' static variables'''
+
'''Show the final shot in the slide.'''
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us explore the differences between''' instance method''' and''' static method.'''
+
  
 +
'''(Three students enrolled and total count as 3)'''
 +
| 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 see how to use''' static methods''' with an example.
  
'''Instance methods''' can access''' static variables, '''whereas
 
  
 +
The example is very similar to the one which is used in the '''Static Variable''' tutorial.
  
A''' static method''' can directly access and modify only''' static variables.'''
 
  
|-
+
Here again we are representing the''' StudentEnroll class'''.
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 8(A)'''
+
 
+
* '''Instance methods''' are invoked only by an''' object'''
+
* A''' static method''' can be invoked
+
 
+
directly without creating an''' object'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Instance methods''' are invoked only by an''' object''' whereas
+
 
+
 
+
A''' static method''' can be invoked directly without creating an''' object.'''
+
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 8(B)'''
+
| 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;"| Highlight '''name''' and '''id'''
 
+
| 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;"|Recall that the variables''' name''' and''' id''' are handled as''' instance variables.'''
* We cannot use''' ‘this’''' and''' ‘super’'''
+
 
+
keyword inside a''' static method.'''
+
 
+
* These keywords refer to the instance of a particular class.
+
* In a static context we can’t refer to instances of a class.
+
 
+
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * We cannot use''' ‘this’''' and''' ‘super’ '''keyword inside a''' static method.'''
+
 
+
* This is because these keywords refer to the instance of a particular class.
+
* In a static context we can’t refer to instances of a class.
+
 
+
 
+
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 9'''
+
| 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;"| Highlight '''organization''' and''' total count'''
 +
| 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;"|Here the variables''' organization''' and''' total count''' are common to the entire '''class'''.
  
'''Static Method - Sample Code'''
 
  
Highlight the code
+
So they can be treated as '''static variables'''.
 
+
public static void setOrgName(String org)
+
 
+
{
+
 
+
orgname=org;
+
 
+
}
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us look at the sample code for a''' static method.'''
+
 
+
If the''' StudentEnroll''' class requires a method to change the value of the '''organisation name.'''
+
 
+
It can be defined as a''' static method''' as shown.
+
 
+
The''' setOrgName''' method represented here is a''' static method''' which can modify the value of''' orgname'''.
+
 
+
 
+
 
+
 
+
|-
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 10'''
+
 
+
'''Invoking Static Methods'''
+
 
+
* A static method can be invoked directly by the class name itself
+
 
+
Highlight '''setOrgName'''
+
 
+
 
+
Highlight the full code
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A '''static''' '''method''' can be invoked directly by the class name itself
+
 
+
For Example, the''' setOrgName''' method can be invoked as
+
 
+
'''StudentEnroll.setOrgName within brackets IIT Mumbai.'''
+
 
+
|-
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In Eclipse IDE, create a project '''StaticMethodDemo'''
+
 
+
 
+
 
+
| style="border: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''' StaticMethodDemo'''.
+
 
+
 
+
Inside this project we will create the necessary classes to demonstrate the usage of''' Static methods.'''
+
 
+
|-
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on''' src''' folder and click '''New'''->''' Class '''and type the class name as''' StudentEnroll''' and hit '''Enter'''
+
 
+
 
+
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We will create a new''' class StudentEnroll.'''
+
  
 
|-
 
|-
Line 258: Line 132:
 
private static String orgname="IIT Bombay";
 
private static String orgname="IIT Bombay";
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now type the following code to represent the''' StudentEnroll''' class.
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now type the following code to represent the''' StudentEnroll''' class.
 
 
  
  
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight''' static int count''' and '''static String orgname'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight''' static int count''' and '''static String orgname'''
 
 
Highlight''' static String orgname'''
 
 
Highlight '''static String orgname="IIT Bombay";'''
 
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Note that there are two''' static variables count''' and''' orgname'''.
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Note that there are two''' static variables count''' and''' orgname'''.
  
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Highlight''' static String orgname'''
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Also note that''' orgname''' is not a '''static constant''' rather it is normal '''static variable.'''
  
Also note that''' orgname''' is not a '''static constant''' rather it is normal '''static variable'''
+
|-
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Highlight '''static String orgname="IIT Bombay";'''
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The''' static variable orgname''' is initialized as''' “IIT Bombay”.'''
The''' static variable orgname''' is initialized as''' “IIT Bombay”'''
+
 
+
 
+
 
+
  
 
|-
 
|-
 
| 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'''
 
| 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'''
| 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'''
+
| 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.'''
  
 
|-
 
|-
Line 290: Line 156:
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Inside the '''constructor''', type '''count++;'''  
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Inside the '''constructor''', type '''count++;'''  
 
  
  
Line 297: Line 162:
 
'''count ++ semicolon'''
 
'''count ++ semicolon'''
  
So, the '''count''' value is incremented every time an object is created.
+
So, the '''count''' value is incremented every time an '''object''' is created.
  
 
|-
 
|-
Line 311: Line 176:
  
 
}
 
}
| 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 values of 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 values of 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''' and''' organisation name.'''
 
Within brackets type the following code to print the values of''' id, name''' and''' organisation name.'''
 
 
 
  
 
|-
 
|-
Line 330: Line 192:
  
 
}
 
}
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will add the''' static method setOrgName''' which can change the value of''' orgname.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will add the''' static method setOrgName.'''
  
 
Type the following code.  
 
Type the following code.  
  
 +
The '''setOrgName method''' represented here is a '''static method''' which can modify the value of '''orgname'''.
 +
 +
Any '''method''' which is used to handle''' static variables''' can be defined as a''' static method.'''
 +
 +
|-
 +
| 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;"| Now let us explore the differences between''' instance method''' and''' static method.'''
 +
 +
|-
 +
| 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;"| '''Slide 8'''
 +
 +
'''Instance Vs Static Methods'''
 +
 +
* '''Instance methods''' can access''' static variables'''
 +
* A '''Static method''' can directly access and modify only''' 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;"| '''Instance methods''' can access''' static variables.'''
 +
 +
 +
Whereas a''' static method''' can directly access and modify only''' 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;"| '''Slide 8(A)'''
 +
 +
* '''Instance methods''' are invoked only by an''' object'''
 +
* A''' static method''' can be invoked directly without creating an''' object'''
 +
| 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;"| '''Instance methods''' are invoked only by an''' object.'''
 +
 +
 +
Whereas a''' static method''' can be invoked directly without creating an''' object.'''
 +
 +
|-
 +
| 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;"| '''Slide 8(B)'''
 +
 +
* We cannot use''' ‘this’''' and''' ‘super’''' keyword inside a''' static method.'''
 +
* These keywords refer to the instance of a particular class.
 +
* In a static context we can’t refer to instances of a class.
  
We have already seen this code earlier in a slide.
+
| 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 cannot use''' ‘this’''' and''' ‘super’ '''keyword inside a''' static method.'''
 +
* This is because these '''keywords''' refer to the instance of a particular '''class'''.
 +
* In a static context, we can’t refer to instances of a '''class'''.
  
 
|-
 
|-
Line 349: Line 250:
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Hover the cursor to show the error
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Hover the cursor to show the error
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now an error comes up in eclipse.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now an error comes up in '''Eclipse'''.
  
 
It indicates that an''' instance variable''' cannot be accessed directly inside a''' static method.'''
 
It indicates that an''' instance variable''' cannot be accessed directly inside a''' static method.'''
Line 377: Line 278:
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| 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;"| 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;"| Now right click on the''' default package''' click''' new'''->''' class''' and then type the 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 the 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 #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We will create a few objects of''' StudentEnroll class''' to represent student enrollments.
  
 
|-
 
|-
Line 397: Line 302:
 
StudentEnroll s3=new StudentEnroll("CS101","CAROL");
 
StudentEnroll s3=new StudentEnroll("CS101","CAROL");
  
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|So type the following code to create 3 objects''' s1, s2''' and''' s3.'''
  
(Copy the code)
+
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| (Copy the code)
  
 
s1.showData();
 
s1.showData();
Line 405: Line 312:
  
 
s3.showData();
 
s3.showData();
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Now let us invoke the''' showData''' method to print the enrollment details.
  
 +
Type the following code to invoke''' showData''' method on''' s1, s2''' and''' s3.'''
  
(Copy and paste)
+
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|(Copy and paste)
  
 
StudentEnroll.showOrgData();
 
StudentEnroll.showOrgData();
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We will create a few objects of''' StudentEnroll class''' to represent student enrollments.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Let us also invoke the '''method showOrgData''' to print the values of''' orgname''' and''' count.'''
  
So type the following code to create 3 objects''' s1, s2''' and''' s3.'''
 
  
 
+
Since it is a''' static method''' we can invoke it directly using its '''class''' name.
Now let us invoke the''' showData''' method to print the enrollment details.
+
 
+
 
+
Type the following code to invoke''' showData''' method on''' s1, s2''' and''' s3.'''
+
 
+
 
+
Let us also invoke the method''' showOrgData''' to print the values of''' orgname''' and''' count.'''
+
 
+
 
+
Since it is a''' static method''' we can invoke it directly using its class name.
+
  
  
 
To do so, type this code.
 
To do so, type this code.
 
 
 
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Click on '''run''' button
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Click on '''Run''' button
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us run the''' Demo''' program.
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us run the''' Demo''' program.
 
 
 
  
 
|-
 
|-
Line 443: Line 336:
  
 
'''IT101 ADIL IIT Bombay'''
 
'''IT101 ADIL IIT Bombay'''
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see that the values of the variables corresponding to''' s1''' i.e''' IT101, ADIL''' and '''IIT BOMBAY''' gets printed.
  
 
+
|-
Highlight '''CS101 AMAL IIT Bombay'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight '''CS101 AMAL IIT Bombay'''
  
 
'''CS101 CAROL IIT Bombay'''
 
'''CS101 CAROL IIT Bombay'''
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Similarly the values corresponding to''' s2''' and''' s3''' are also printed.
  
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Box '''IIT BOMBAY'''
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Note that the value of''' orgname''' ie''' IIT BOMBAY''' is common for''' s1, s2''' and''' s3.'''
  
Box '''IIT BOMBAY'''
+
'''orgname''' and''' count''' are printed separately by the '''static method showOrgData.'''
 
+
 
+
Highlight '''Name:IIT Bombay'''
+
  
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight '''Name:IIT Bombay'''
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Note that the organisation name is printed as''' IIT Bombay'''.
  
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
 
Highlight '''Total Students Enrolled:3'''
 
Highlight '''Total Students Enrolled:3'''
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see that the values of the variables corresponding to''' s1''' i.e''' IT101, ADIL''' and
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The value of the number of student enrollments is printed as''' 3,''' as we have already created 3 objects.
  
'''IIT BOMBAY''' gets printed.
+
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A '''static method''' can be invoked directly by the '''class''' name itself.
  
  
Similarly the values corresponding to''' s2''' and''' s3''' are also printed.
+
Now let us invoke the '''static method setOrgName.'''
 
+
 
+
Note that the value of''' orgname''' ie''' IIT BOMBAY''' is common for''' s1, s2''' and''' s3'''
+
 
+
 
+
'''orgname''' and''' count''' are printed separately by the static method''' showOrgData.'''
+
 
+
 
+
Note that the organisation name is printed as''' IIT Bombay'''.
+
 
+
 
+
The value of the number of student enrollments is printed as''' 3,''' as we have already created 3 objects'''.'''
+
  
 
|-
 
|-
Line 480: Line 370:
  
 
StudentEnroll.setOrgName("IIT Mumbai");
 
StudentEnroll.setOrgName("IIT Mumbai");
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We will change the organisation name from''' “IIT Bombay”''' to''' “IIT Mumbai”'''.
  
 +
So type the following code.
  
(Copy the code)
+
|-
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| (Copy the code)
 
s1.showData();
 
s1.showData();
  
Line 489: Line 381:
  
 
s3.showData();
 
s3.showData();
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us once again invoke the''' showData''' method on''' s1, s2''' and''' s3''' to print the enrollment details.
  
  
Type
+
For that type the following code again.
 +
 
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type
  
 
StudentEnroll.showOrgData();
 
StudentEnroll.showOrgData();
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us invoke the static method''' setOrgName.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Once again, let us invoke the '''method showOrgData''' to print the values of''' orgname''' and''' count.'''
 
+
 
+
We will change the organisation name from''' “IIT Bombay”''' to''' “IIT Mumbai”'''.
+
 
+
 
+
So type the following code.
+
 
+
 
+
Now let us once again invoke the''' showData''' method on''' s1, s2''' and''' s3''' to print the enrollment details.
+
 
+
 
+
For that type the following code again'''.'''
+
 
+
 
+
Once again, let us invoke the method''' showOrgData''' to print the values of''' orgname''' and''' count.'''
+
  
  
 
To do so, type this code.
 
To do so, type this code.
 
 
 
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Click on '''run''' button
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Click on '''Run''' button
 
+
 
+
 
+
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now run the '''Demo''' program again.
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now run the '''Demo''' program again.
  
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Box '''IIT Mumbai'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Box '''IIT Mumbai'''
 
 
 
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see that the organisation name is changed to''' “IIT Mumbai”.'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see that the organisation name is changed to''' “IIT Mumbai”.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to slides
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to slides
 
 
 
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us come back to slides.
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us come back to slides.
  
Line 543: Line 412:
 
'''Passing an object reference'''
 
'''Passing an object reference'''
  
* '''Object references '''can be passed to a
+
* '''Object references '''can be passed to a '''static method.'''
 +
* This way a''' static method''' can access the '''instance variables''' of that particular object.
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Object references''' can be passed to a '''static method.'''
  
static method.
 
  
 
+
This way a''' static method''' can access the '''instance variables''' of that particular '''object'''.
* This way a''' static method''' can access
+
 
+
the instance variables of that particular object.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Object references''' can be passed to a static method.
+
 
+
 
+
This way a''' static method''' can access the instance variables of that particular object.
+
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to Eclipse  
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to '''Eclipse'''
  
Go to '''StudentEnroll''' class
+
Go to '''StudentEnroll class'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us try it in our code.  
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us try it in our code.  
  
  
Switch to Eclipse and go to the '''StudentEnroll''' class.
+
Switch to '''Eclipse''' and go to the '''StudentEnroll class'''.
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In the''' setOrgName''' method
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In the '''setOrgName method''',
  
  
Line 573: Line 436:
 
comma''' StudentEnroll s'''
 
comma''' StudentEnroll s'''
  
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now in the''' setOrgName method''', pass another '''argument''' as an''' object''' of''' StudentEnroll class'''.
uncomment''' id = "newid"'''
+
 
+
 
+
Type '''s.id="newid";'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now in the''' setOrgName''' method, pass another argument as an''' object''' of''' StudentEnroll''' class.
+
  
  
Line 585: Line 443:
 
comma''' StudentEnroll s'''
 
comma''' StudentEnroll s'''
  
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| uncomment''' id = "newid"'''
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now inside this '''method''', uncomment''' id = "newid"'''
  
Now inside this method, uncomment''' id = "newid"'''
+
|-
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type '''s.id="newid";'''
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| And instead of '''id, '''type '''s.id'''
And instead of '''id, '''type '''s.id'''
+
 
+
 
+
 
+
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Go to the '''Demo''' class
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Go to the '''Demo class'''  
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now go to the '''Demo class'''.
 
+
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now go to the '''Demo''' class.
+
  
 
|-
 
|-
Line 605: Line 459:
  
 
after '''“IIT Mumbai”''', type comma''' s1'''
 
after '''“IIT Mumbai”''', type comma''' s1'''
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us modify the '''function call''' to''' setOrgName method''' by passing the '''StudentEnroll object s1.'''
 
+
 
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us modify the function call to''' setOrgName method''' by passing the '''StudentEnroll''' object '''s1.'''
+
  
  
 
So here, after '''“IIT Mumbai”''', type comma''' s1.'''
 
So here, after '''“IIT Mumbai”''', type comma''' s1.'''
 
 
 
  
 
|-
 
|-
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Click on '''run''' button
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Click on '''Run''' button
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now run the '''Demo '''program again.
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now run the '''Demo '''program again.
  
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight id '''“newid”'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight id '''“newid”'''
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see that the value of id for''' s1''' has changed to''' “newid”'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see that the value of id for''' s1''' has changed to''' “newid”.'''
 
+
 
+
 
+
  
 
|-
 
|-
Line 635: Line 480:
 
* How to differentiate '''static methods '''and '''instance methods'''
 
* How to differentiate '''static methods '''and '''instance methods'''
 
* How to create and invoke '''static methods'''
 
* How to create and invoke '''static methods'''
 
 
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us summarize.
 
| 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 646: Line 488:
 
* How to create and invoke '''static methods'''
 
* How to create and invoke '''static methods'''
  
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|This assignment is a continuation of the '''Static variable''' assignment.
  
  
|-
+
Make sure that you have completed the '''Static variable''' assignment.
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 13'''
+
  
* Design a class CarService to represent
 
  
a Car Service Station
+
We will highlight only the modifications here.
  
* This class should contain variables to
+
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 13'''
  
represent the following
+
* Design a class CarService to represent a Car Service Station
 
+
* This class should contain variables to represent the following
* Name of the Service Station
+
** Name of the Service Station
* Car make, model, regno and status
+
** Car make, model, regno and status
* No of Cars in for Service
+
** No of Cars in for Service
* No of cars out after Service
+
** No of cars out after Service
  
 
'''( Highlight “status” )'''
 
'''( Highlight “status” )'''
Line 668: Line 512:
  
 
'''( Highlight “No of cars out after service” )'''
 
'''( Highlight “No of cars out after service” )'''
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For the Assignment,
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Here we have a variable to represent “'''status”.'''
 
+
 
+
This assignment is a continuation of the static variable assignment.
+
 
+
 
+
Make sure that you have completed the '''Static variable''' assignment.
+
 
+
 
+
We will highlight only the modifications here.
+
 
+
 
+
Here we have a variable to represent “'''status”.'''
+
  
  
Line 689: Line 521:
  
 
'''No of cars out after Service'''
 
'''No of cars out after Service'''
 
 
 
  
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 13 A'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 13 A'''
  
* Define a method''' service( Car c)'''
+
* Define a '''method service( Car c)''' which updates the status to''' ”out”'''
 
+
which updates the status to''' ”out”'''
+
  
  
 
Accordingly it modifies the values for
 
Accordingly it modifies the values for
 
 
 
* '''No of Cars in''' for Service
 
* '''No of Cars in''' for Service
 
* '''No of Cars out''' after Service
 
* '''No of Cars out''' after Service
 
+
* Define a method''' show( )''' to print the car details
* Define a method''' show( )''' to print the
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Define a '''method service( Car c) '''which updates the status to''' ”out”.'''
 
+
car details
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Define a method''' service( Car c) '''which updates the status to''' ”out”'''
+
  
  
 
Accordingly it modifies the values for
 
Accordingly it modifies the values for
 
 
 
* '''No of Cars in''' for Service
 
* '''No of Cars in''' for Service
 
* '''No of Cars out''' after Service
 
* '''No of Cars out''' after Service
  
Also define a method''' show( )''' to print all the car details
+
Also define a method''' show( )''' to print all the car details.
 
+
 
+
 
+
  
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 13 B'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 13 B'''
  
* Identify the''' instance variables''' and
+
* Identify the''' instance variables''' and '''static variables'''
 
+
'''static variables'''
+
 
+
  
 
* Also Identify the''' instance methods''' and''' static methods'''
 
* Also Identify the''' instance methods''' and''' static methods'''
  
* Define a''' constructor''' to initialise the
+
* Define a''' constructor''' to initialise the values for Car''' make, model, regno''' and''' status'''
 
+
values for Car''' make, model, regno''' and''' status'''
+
  
  
 
'''( Highlight “static method” )'''
 
'''( Highlight “static method” )'''
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As before,
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As before, we need to perform the following as listed.
  
  
We need to perform the following as listed
+
Note that we have to identify and define the '''static method''' as required.
 
+
 
+
Note that we have to identify and define the '''static method''' as required
+
  
 
|-
 
|-
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 13 C'''
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 13 C'''
  
* Also create a''' Demo''' class containing
+
* Also create a''' Demo class''' containing the '''main method'''
 
+
the main method
+
 
+
 
+
Inside the main method
+
  
  
 +
Inside the '''main method'''-
 
* Create a few objects of''' CarService'''
 
* Create a few objects of''' CarService'''
  
* Invoke the''' service( )''' method on some
+
* Invoke the''' service( ) method''' on some of them
  
of them
+
* Invoke the''' show( ) method''' using all the objects and verify the results
  
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Also create a''' Demo class'''.
  
* Invoke the''' show( )''' method using all the objects and verify the results
 
 
 
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Also create a''' Demo''' class.
 
 
 
Inside the main method
 
  
 +
Inside the '''main method''',
  
 
Create a few objects of''' CarService.'''
 
Create a few objects of''' CarService.'''
  
 +
Invoke the''' service( ) method''' on some of them.
  
Invoke the''' service( )''' method on some of them.
+
Invoke the''' show( ) method''' using all the objects and verify the results.
 
+
 
+
Invoke the''' show( )''' method using all the
+
 
+
objects and verify the results.
+
 
+
 
+
 
+
  
 
|-
 
|-
Line 825: Line 618:
 
| 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 #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This script has been contributed by:
  
'''Dept. of Information'''
+
'''Dept. of Information Technology, Amal Jyothi College of Engineering.'''
 
+
'''Technology, Amal Jyothi College of Engineering.'''
+
  
  

Latest revision as of 11:20, 3 January 2017

Title of script: Static Methods

Author: Joms Antony

Keywords: Static methods, Class methods, Instance methods, Static variables


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

Learning Objectives

  • What are static methods
  • Defining static methods
  • Difference between instance methods and static methods
  • How to use static methods
In this tutorial we will learn:
  • What are static methods
  • Defining static methods
  • Difference between instance methods and static methods and
  • How to use static methods
Slide 3

Software Requirements

  • Ubuntu 14.04
  • JDK 1 .7
  • Eclipse 4.3.1
Here we are using
  • Ubuntu 14.04
  • JDK 1 .7 and
  • Eclipse 4.3.1
Slide 4

Prerequisites

  • Basic knowledge of Java and Eclipse IDE
  • Knowledge of instance variables, methods and static variables in Java
  • 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 instance variables, methods and static variables in Java.

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

Slide 5

Static Method


  • A static method is a method which is associated with the entire class
  • It is also called a class method and is declared using the static keyword.
  • Static methods are usually used to handle static variables


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


It is also called a class method and is declared using the static keyword.


Static methods are usually used to handle static variables.

In Eclipse IDE, create a project StaticMethodDemo


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


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

Right click on src folder and click New-> Class and type the class name as StudentEnroll and hit Enter We will create a new class StudentEnroll.
Slide 6

Static Method – Example


Show the final shot in the slide.

(Three students enrolled and total count as 3)

Let us see how to use static methods with an example.


The example is very similar to the one which is used in the Static Variable tutorial.


Here again we are representing the StudentEnroll class.

Highlight name and id Recall that the variables name and id are handled as instance variables.
Highlight organization and total count Here the variables organization and total count are common to the entire class.


So they can be treated as static variables.

//copy and paste the code inside the class and highlight them

private String id, name;

private static int count;

private static String orgname="IIT Bombay";

Now type the following code to represent the StudentEnroll class.


Highlight static int count and static String orgname Note that there are two static variables count and orgname.
Highlight static String orgname Also note that orgname is not a static constant rather it is normal static variable.
Highlight static String orgname="IIT Bombay"; The static variable orgname is initialized as “IIT Bombay”.
click on Source -> and select Generate Constructor using Fields Now click on Source -> and select Generate Constructor using Fields.
Delete the super keyword Delete the super keyword from the generated code.
Inside the constructor, type count++;


Inside the constructor, type

count ++ semicolon

So, the count value is incremented every time an object is created.

Type

public void showData()

//copy the code and highlight it

{

System.out.println(id+" "+name+" "+orgname);

}

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

So type public void showData( ).

Within brackets type the following code to print the values of id, name and organisation name.

//copy and paste the code

public static void setOrgName(String org)

{

orgname=org;

}

Now we will add the static method setOrgName.

Type the following code.

The setOrgName method represented here is a static method which can modify the value of orgname.

Any method which is used to handle static variables can be defined as a static method.

Now let us explore the differences between instance method and static method.
Slide 8

Instance Vs Static Methods

  • Instance methods can access static variables
  • A Static method can directly access and modify only static variables
Instance methods can access static variables.


Whereas a static method can directly access and modify only static variables.

Slide 8(A)
  • Instance methods are invoked only by an object
  • A static method can be invoked directly without creating an object
Instance methods are invoked only by an object.


Whereas a static method can be invoked directly without creating an object.

Slide 8(B)
  • We cannot use ‘this’ and ‘super’ keyword inside a static method.
  • These keywords refer to the instance of a particular class.
  • In a static context we can’t refer to instances of a class.
  • We cannot use ‘this’ and ‘super’ keyword inside a static method.
  • This is because these keywords refer to the instance of a particular class.
  • In a static context, we can’t refer to instances of a class.
Inside this static method type

id = “newid”;

Let us see what happens if we try to access an instance variable directly inside this static method.

Type,

id= “newid” semicolon

Hover the cursor to show the error Now an error comes up in Eclipse.

It indicates that an instance variable cannot be accessed directly inside a static method.

Comment the statement So let us comment this line and proceed.
//Copy and paste the code and highlight according to narration

public static void showOrgData()

{

System.out.println("\nORGANISATION DATA");

System.out.println("Name:"+orgname);

System.out.println("Total Students Enrolled:"+count);

}

Now we will add one more static method showOrgData.


These statements print the values of orgname and count.

click New-> Class and then type name as Demo. Now right click on the default package, click New-> Class and then type the 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.

We will create a few objects of StudentEnroll class to represent student enrollments.
//Copy the code to create objects

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


StudentEnroll s2=new StudentEnroll("CS101","AMAL");


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

So type the following code to create 3 objects s1, s2 and s3.
(Copy the code)

s1.showData();

s2.showData();

s3.showData();

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

Type the following code to invoke showData method on s1, s2 and s3.

(Copy and paste)

StudentEnroll.showOrgData();

Let us also invoke the method showOrgData to print the values of orgname and count.


Since it is a static method we can invoke it directly using its class name.


To do so, type this code.

Click on Run button Now let us run the Demo program.
Highlight

IT101 ADIL IIT Bombay

We can see that the values of the variables corresponding to s1 i.e IT101, ADIL and IIT BOMBAY gets printed.
Highlight CS101 AMAL IIT Bombay

CS101 CAROL IIT Bombay

Similarly the values corresponding to s2 and s3 are also printed.
Box IIT BOMBAY Note that the value of orgname ie IIT BOMBAY is common for s1, s2 and s3.

orgname and count are printed separately by the static method showOrgData.

Highlight Name:IIT Bombay Note that the organisation name is printed as IIT Bombay.

Highlight Total Students Enrolled:3

The value of the number of student enrollments is printed as 3, as we have already created 3 objects.
A static method can be invoked directly by the class name itself.


Now let us invoke the static method setOrgName.

Type

StudentEnroll.setOrgName("IIT Mumbai");

We will change the organisation name from “IIT Bombay” to “IIT Mumbai”.

So type the following code.

(Copy the code)

s1.showData();

s2.showData();

s3.showData();

Now let us once again invoke the showData method on s1, s2 and s3 to print the enrollment details.


For that type the following code again.

Type

StudentEnroll.showOrgData();

Once again, let us invoke the method showOrgData to print the values of orgname and count.


To do so, type this code.

Click on Run button Now run the Demo program again.
Box IIT Mumbai We can see that the organisation name is changed to “IIT Mumbai”.
Switch to slides Let us come back to slides.
Slide 11

Passing an object reference

  • Object references can be passed to a static method.
  • This way a static method can access the instance variables of that particular object.
Object references can be passed to a static method.


This way a static method can access the instance variables of that particular object.

Switch to Eclipse

Go to StudentEnroll class

Let us try it in our code.


Switch to Eclipse and go to the StudentEnroll class.

In the setOrgName method,


After String org, type

comma StudentEnroll s

Now in the setOrgName method, pass another argument as an object of StudentEnroll class.


So after String org, type

comma StudentEnroll s

uncomment id = "newid" Now inside this method, uncomment id = "newid"
Type s.id="newid"; And instead of id, type s.id
Go to the Demo class Now go to the Demo class.
In the StudentEnroll.setOrgName("IIT Mumbai");

after “IIT Mumbai”, type comma s1

Let us modify the function call to setOrgName method by passing the StudentEnroll object s1.


So here, after “IIT Mumbai”, type comma s1.

Click on Run button Now run the Demo program again.
Highlight id “newid” We can see that the value of id for s1 has changed to “newid”.
Slide 12

Summary

  • What is a static method and when it is used
  • How to differentiate static methods and instance methods
  • How to create and invoke static methods
Let us summarize.

In this tutorial we have learnt about

  • What is a static method and when it is used
  • How to differentiate static methods and instance methods and
  • How to create and invoke static methods
This assignment is a continuation of the Static variable assignment.


Make sure that you have completed the Static variable assignment.


We will highlight only the modifications here.

Slide 13
  • Design a class CarService to represent a Car Service Station
  • This class should contain variables to represent the following
    • Name of the Service Station
    • Car make, model, regno and status
    • No of Cars in for Service
    • No of cars out after Service

( Highlight “status” )


( Highlight “No of cars out after service” )

Here we have a variable to represent “status”.


It is used to indicate whether the car is “in” for service or “out” after service


We will also have another variable to represent

No of cars out after Service

Slide 13 A
  • Define a method service( Car c) which updates the status to ”out”


Accordingly it modifies the values for

  • No of Cars in for Service
  • No of Cars out after Service
  • Define a method show( ) to print the car details
Define a method service( Car c) which updates the status to ”out”.


Accordingly it modifies the values for

  • No of Cars in for Service
  • No of Cars out after Service

Also define a method show( ) to print all the car details.

Slide 13 B
  • Identify the instance variables and static variables
  • Also Identify the instance methods and static methods
  • Define a constructor to initialise the values for Car make, model, regno and status


( Highlight “static method” )

As before, we need to perform the following as listed.


Note that we have to identify and define the static method as required.

Slide 13 C
  • Also create a Demo class containing the main method


Inside the main method-

  • Create a few objects of CarService
  • Invoke the service( ) method on some of them
  • Invoke the show( ) method using all the objects and verify the results
Also create a Demo class.


Inside the main method,

Create a few objects of CarService.

Invoke the service( ) method on some of them.

Invoke the show( ) method using all the objects and verify the results.

About Project

(retain the slide as in TEX file)

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


Please download and watch it.

About Workshops

(retain the slide as in TEX file)

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

(retain the slide as in TEX file)

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

(retain the slide as in TEX file)

This script has been contributed by:

Dept. of Information Technology, Amal Jyothi College of Engineering.


This is Priya from IIT Bombay, thanks for joining.

Contributors and Content Editors

Nancyvarkey, Priyacst