Difference between revisions of "Java/C3/Static-Methods/English"
(Created page with "'''Title of script:''' Static Methods '''Author:''' Joms Antony '''Keywords:''' Static methods, Class methods, Instance methods, Static variables {| style="border-spacing:...") |
|||
Line 92: | Line 92: | ||
|- | |- | ||
− | | 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''' |
+ | |||
+ | |||
+ | |||
+ | | 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.''' | ||
+ | |||
+ | |- | ||
+ | | 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''' | ||
'''Static Method – Example''' | '''Static Method – Example''' | ||
Line 109: | Line 123: | ||
− | | 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'''.''' | + | | 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'''.''' |
− | + | The example is very similar to the one which is used in the '''Static Variable''' tutorial. | |
Line 125: | Line 139: | ||
So they can be treated as '''static variables'''. | So they can be treated as '''static variables'''. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 272: | Line 163: | ||
− | 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.''' |
− | The''' static variable orgname''' is initialized as''' “IIT Bombay”''' | + | The''' static variable orgname''' is initialized as''' “IIT Bombay”.''' |
Line 282: | Line 173: | ||
|- | |- | ||
| 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 316: | Line 207: | ||
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 218: | ||
} | } | ||
− | | 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 | + | | 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;"| '''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;"| Now let us explore the differences between''' instance method''' and''' static method.''' | ||
+ | |||
+ | |||
+ | '''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. | ||
+ | |||
+ | |||
+ | | 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 378: | Line 317: | ||
|- | |- | ||
| 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.''' |
Line 457: | Line 396: | ||
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;"| We can see that the values of the variables corresponding to''' s1''' i.e''' IT101, ADIL''' and '''IIT BOMBAY''' gets printed. |
− | + | ||
− | '''IIT BOMBAY''' gets printed. | + | |
Line 465: | Line 402: | ||
− | Note that the value of''' orgname''' ie''' IIT BOMBAY''' is common for''' s1, s2''' and''' s3''' | + | Note that the value of''' orgname''' ie''' IIT BOMBAY''' is common for''' s1, s2''' and''' s3.''' |
Line 494: | Line 431: | ||
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;"| A '''static method''' can be invoked directly by the class name itself. |
+ | |||
+ | |||
+ | Now let us invoke the static method''' setOrgName.''' | ||
Line 513: | Line 453: | ||
To do so, type this code. | To do so, type this code. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 622: | Line 559: | ||
|- | |- | ||
| 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 710: | Line 647: | ||
car details | 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”''' | + | | 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”.''' |
Line 719: | Line 656: | ||
* '''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. |
Line 746: | Line 683: | ||
− | 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. |
|- | |- | ||
Line 772: | Line 709: | ||
− | Inside the main method | + | Inside the main method, |
Revision as of 10:41, 2 January 2017
Title of script: Static Methods
Author: Joms Antony
Keywords: Static methods, Class methods, Instance methods, Static variables
|
|
Slide 1 | Welcome to the Spoken Tutorial on Static Methods. |
Slide 2
Learning Objectives
|
In this tutorial we will learn:
|
Slide 3
Software Requirements
|
Here we are using
|
Slide 4
Prerequisites
|
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.
|
In Eclipse IDE, create a project StaticMethodDemo
|
Now we will switch to Eclipse and create a new project called StaticMethodDemo.
|
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
(Three students enrolled and total count as 3)
|
Let us see how to use static methods with an example.
|
//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
Highlight static String orgname="IIT Bombay"; |
Note that there are two static variables count and orgname.
|
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.
|
Slide 8
Instance Vs Static Methods
and modify only static variables |
Now let us explore the differences between instance method and static method.
|
Slide 8(A)
directly without creating an object |
Instance methods are invoked only by an object whereas
|
Slide 8(B)
keyword inside a static method.
|
* We cannot use ‘this’ and ‘super’ keyword inside a static method.
|
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.
|
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.
|
//Copy the code to create objects
StudentEnroll s1=new StudentEnroll("IT101","ADIL");
s1.showData(); s2.showData(); s3.showData();
StudentEnroll.showOrgData(); |
We will create a few objects of StudentEnroll class to represent student enrollments.
So type the following code to create 3 objects s1, s2 and s3.
|
Click on run button | Now let us run the Demo program.
|
Highlight
IT101 ADIL IIT Bombay
CS101 CAROL IIT Bombay
|
We can see that the values of the variables corresponding to s1 i.e IT101, ADIL and IIT BOMBAY gets printed.
|
Type
StudentEnroll.setOrgName("IIT Mumbai");
s1.showData(); s2.showData(); s3.showData();
StudentEnroll.showOrgData(); |
A static method can be invoked directly by the class name itself.
|
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
static method.
the instance variables of that particular object. |
Object references can be passed to a static method.
|
Switch to Eclipse
Go to StudentEnroll class |
Let us try it in our code.
|
In the setOrgName method
comma StudentEnroll s
|
Now in the setOrgName method, pass another argument as an object of StudentEnroll class.
comma StudentEnroll s
|
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.
|
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
|
Let us summarize.
|
Slide 13
a Car Service Station
represent the following
( Highlight “status” )
|
For the Assignment,
No of cars out after Service
|
Slide 13 A
which updates the status to ”out”
car details |
Define a method service( Car c) which updates the status to ”out”.
Also define a method show( ) to print all the car details.
|
Slide 13 B
static variables
values for Car make, model, regno and status
|
As before,
|
Slide 13 C
the main method
of them
|
Also create a Demo class.
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.
|
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
|
About NMEICT
(retain the slide as in TEX file) |
Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.
|
Contributor slide
(retain the slide as in TEX file) |
This script has been contributed by:
Dept. of Information Technology, Amal Jyothi College of Engineering.
|