Java/C3/Static-Methods/English
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.
|