0% found this document useful (0 votes)
29 views8 pages

Static Keyword, Variables and Static Methods: C. Vybbhavee 23R11A0599

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views8 pages

Static Keyword, Variables and Static Methods: C. Vybbhavee 23R11A0599

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

STATIC KEYWORD, VARIABLES AND

STATIC METHODS

C.Vybbhavee
23R11A0599
STATIC K EYWORD

• • The static keyword is a fundamental concept in programming languages such


as Java, C++, and C#.
• • It is used to define class-level variables and methods that belong to the class
itself rather than to any specific instance.
• • Understanding static variables and methods is crucial for efficient memory
management and code organization.
STATIC VARIABLES

• • Static variables are shared among all instances of a class, meaning they have a
single copy for all objects.
• • They are initialized only once, when the class is loaded, and retain their values
across all instances.
• • This feature makes static variables ideal for constants or settings that should
be consistent across all instances.
EX AMPLE PROGRAM FOR STATIC
VARIABLE S
class Example {
// Static variable
static int count = 0;
// Constructor to increment count
public Example() {
count++;
} // Static method to display the count
public static void displayCount() {
System.out.println("Count: " + count);
}
}
public class StaticDemo {
public static void main(String[] args) {
// Create two objects of the Example class
new Example();
new Example();
// Call the static method to display the count
Example.displayCount(); // Output: Count: 2
}}
STATIC METHODS

• • Static methods are functions that belong to the class itself and not to any
specific instance of the class.
• • They can access static variables and other static methods directly, but cannot
access instance variables or methods without an object reference.
• • Static methods are commonly used for utility functions that perform
operations without needing to modify instance data.
EX AMPLE PROGRAM FOR STATIC
METHODS

public class StaticMethodExample {


// Static method to calculate the square of a number
public static int square(int number) {
return number * number;
}
public static void main(String[] args) {
int num = 5; // Example number
int result = square(num); // Calling the static method
System.out.println(“The square of “ + num + “ is “ + result);
}
• }
LIMITATIONS OF STATIC VARIABLES AND
METHODS

• • One limitation of static variables is that they cannot be overridden in derived


classes, which may restrict polymorphism.
• • Static methods cannot access instance-specific data, which may limit their
usability in certain contexts.
• • Overusing static variables and methods can lead to tightly coupled code,
making it harder to maintain and test.
THANK YOU

You might also like