0% found this document useful (0 votes)
0 views

Week 09

The document provides an overview of static methods and variables in programming, particularly in Java. It explains that static variables are shared across all instances of a class and can be accessed without creating an object, while static methods can only access static data. Examples illustrate how static methods and variables function within the same and different classes.

Uploaded by

40096
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Week 09

The document provides an overview of static methods and variables in programming, particularly in Java. It explains that static variables are shared across all instances of a class and can be accessed without creating an object, while static methods can only access static data. Examples illustrate how static methods and variables function within the same and different classes.

Uploaded by

40096
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Contact Information

Instructor: Engr. Khateeb Khan


Lecturer (Software Engineering)
Department of Computer Science & Information Technology
IQRA University, Islamabad
•Email:[email protected]
•Office hours:
ACADEMIC BACKGROUND

BS-SE (MUST) MIRPUR, AJK

MS-SE(UET) TAXILA, PAKISTAN

PHD-SE(UET) TAXILA, PAKISTAN ( IN PROGRESS)


1
Content

Static Methods (Remaining)


CASE STUDY

2
Content

A static variable is a variable in computer programming that is allocated during compilation

and is available for the entire duration of a program's run. Static variables are shared across

all instances of a class, and are similar to global variables.

Static variables are used with the class name and the dot operator, since they are associated

with a class, not objects of a class. Static methods cannot access or change the values of

instance variables, but they can access or change the values of static variables.

3
Static Method

A static method in Java is a method that is part of a class rather than an instance of that class.

Every instance of a class has access to the method.

Static methods have access to class variables (static variables) without using the class’s object (instance).

Only static data may be accessed by a static method. It is unable to access data that is not static (instance

variables).

In both static and non-static methods, static methods can be accessed directly.

4
Static Method

5
How to use a static method

6
How to use a static method

7
Static variables and static methods are in the same class

A static method can access any static variable or static method without any object creation.

class StaticMethodExample
{ public static void main(String arg[])
int a = 0; {
static int b = 0; displayData();
void showData() StaticMethodExample obj = new
{ StaticMethodExample();
System.out.println("Value of a : "+ a); obj.showData();
System.out.println("Value of b : "+ b); }
} }
static void displayData()
{
StaticMethodExample obj = new
StaticMethodExample();
System.out.println("Value of a : "+ obj.a);
System.out.println("Value of b : "+ b);
}
8
Static variables and static methods are in the same class

A static method can access any static variable or static method without any object creation.

9
Static variables and static methods in Different Class

A static method can access any static variable or static method without any object creation.

class StaticExample public class MainClass


{ {
int a = 0; public static void main(String arg[])
static int b = 0; {
static String displayData() System.out.println("Value of b :"+
{ StaticExample.b);
return "Static method"; System.out.println(StaticExample.displayData());
} }
} }

10

You might also like