Java Tutorial [01.04] static and final keywords in Java

static and final keywords in Java

In Java, static and final are two commonly used keywords used to define the characteristics of classes, methods and variables. In this tutorial, we will learn how to use static and final keywords and what they are used for.

static keyword

The static keyword is used to create static members, which means they belong to the class itself rather than an instance of the class. By using the static keyword, we can access and use these members without creating an instance of the class.

1. Static variables

A static variable is a variable that belongs to the class, not an instance of the class. They are created when the class is loaded and remain consistent throughout the execution of the program.

public class MyClass {
   
    
    
    public static int count;
}

In the above example, we have created a static variable countwhich belongs to MyClassthe class.

2. Static methods

A static method is a method that belongs to a class

Guess you like

Origin blog.csdn.net/IamBird/article/details/131065140