Day 17:
Static Keyword:
Static is a keyword in Java. The static keyword is used to indicate that a particular member (method, variable, block, or nested class) belongs to the class rather than to any specific object of the class. This means that the static member can be accessed without creating an instance/Object.
Where static Keyword can be used
1. Static Variables
A static variable is shared among all instances of the class. It is initialized only once when the class is loaded into memory.
The count variable is shared, so its value is incremented across all instances, irrespective of constructor, static methods or non static methods inside a class . It's a class related variable rather than method.
Output of above is: Number of objects created: 3
2. Static Methods
A static method belongs to the class and can be called without creating an object. It can only access other static members (variables and methods).
Output:
Hello from static method!
If we create an object for a static method and we try to access, we will get a compile time warning but will still be executed The static method displayMessage() from the type StaticMethodExample should be accessed in a static way.
This is because static methods belong to the class, not any instance of the class, and accessing them through an object is redundant and can confuse readers of the code.
Best practice is to access static methods and variables using the class name to avoid warnings and maintain code clarity.
3. Static Blocks
A static block is used to initialize static variables or execute code that should run once when the class is loaded into memory. They can be used for static initialization of a class. This code inside the static block is executed only once: the first time the class is loaded into memory.?
Example
The static block runs once when the class is loaded, even before the main method, so it need not be called executed separately.
Output:
领英推荐
Static block executed.
Value of num: 42
similar to this main method which itself a static allowing the JVM to call it without creating an object of the class.
No this or super allowed for static
Static Methods Don't Have Access to this or super in Java, Before that let us understand what is this and super.
this Keyword:
We saw in previous classes let's relook on it.
Instance Variable
Example:
this in Static Methods
If you try to use this in a static method, you’ll encounter a compile-time error because the static method doesn't have an instance to refer to.
super Keyword:
super in Static Methods
Similarly, if you attempt to use super in a static method, you’ll get a compile-time error because super refers to a parent class in the context of an instance, which static methods lack.
Error message is Cannot use 'super' in a static context.
When to Use static
Understanding static is essential for designing efficient and scalable Java applications, especially for utility classes and shared resources. Let's start exploring about string tomorrow.
Happy learning :)