PUBLIC VS STATIC METHOD IN JAVA

 PUBLIC VS STATIC METHOD IN JAVA .....


in Java, the keywords public and static have different meanings and are used for different purposes.

➡ The public keyword is an access modifier that is used to specify the visibility of a class, method, or variable. When a class, method, or variable is declared as public, it can be accessed from anywhere in the program. This means that other classes or packages can access the public member and use it in their code. For example, a public method in a class can be called by any other class that has an instance of that class.

On the other hand, the static keyword is used to declare a class-level or static variable, method, or block of code. When a variable or method is declared as static, it becomes associated with the class rather than with any particular instance of the class. This means that a static method can be called without creating an instance of the class, and a static variable has the same value for all instances of the class. For example, a static method in a class can be called using the class name, without creating an instance of the class.

In summary, the public keyword is used to specify the visibility of a class, method, or variable, while the static keyword is used to declare a class-level or static variable, method, or block of code.


Post a Comment

0 Comments