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. Variables
  2. Methods
  3. Blocks

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:

  • Refers to the current instance of a class.
  • Used to access instance variables or methods within the same object.

We saw in previous classes let's relook on it.

Instance Variable

  • The variables that are declared inside the class but outside the scope of any method are called instance variables in Java.
  • The instance variable is initialized at the time of the class loading or when an object of the class is created.
  • An instance variable can be declared using different access modifiers available in Java like default, private, public, and protected.
  • Instance variables of different types have default values that are specified in the next point.

Example:

example of default instancevar is here.


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:

  • Refers to the parent class (superclass) of the current object.
  • Used to access methods and variables of the parent class.

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

  1. For constants (static final).
  2. Utility methods (e.g., Math class methods).
  3. Shared resources (e.g., connection pools).
  4. To create factory methods or singleton instances.

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 :)

要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Arrays

    Arrays

    Been so long since I posted, Happy that i'm able to be back with basic concepts again. Let's see what is Arrays: As we…

  • Day 19: StringBuffer and StringBuilder in Java:

    Day 19: StringBuffer and StringBuilder in Java:

    Mutable String: A String that can be modified or changed is known as mutable String. and are classes in Java used for…

  • Day 18: Strings:

    Day 18: Strings:

    String is a class in the java.lang package and is one of the most commonly used classes.

  • Day 16: Jump Statements

    Day 16: Jump Statements

    Jump Statements These statements alter the normal flow of control by transferring execution. 1.

  • Day 16:

    Day 16:

    Looping Statements Looping statement are the execution of the block of code repeatedly until we break it. for Loop…

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • Day 14: Encapsulation

    Day 14: Encapsulation

    Day 14: Encapsulation: Encapsulation in Java is integrating the data (fields) and the methods that operate on the data…

  • DAY 13 Polymorphism:

    DAY 13 Polymorphism:

    What is Polymorphism? In simple terms, polymorphism means "many forms," enabling a single method to work in different…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

  • DAY11 Multiple Inheritance and Interfaces:

    DAY11 Multiple Inheritance and Interfaces:

    4. Multiple Inheritance Java does not support multiple inheritance with classes to avoid ambiguity caused by the…

社区洞察

其他会员也浏览了