0
About Static and Public Key word
Where is the difference between "static" And "public" key word?
4 Answers
+ 10
#2
Variables and method are also called "members". A member that belongs to the instance, is called instance member; a member that belongs to the class (a static variable or method) is called class member.
Besides instance members there are also static members, or class members. Class variables and methods are created by adding the static keyword to a method or variable.
public class Person {
private Name personName;
private static Planet homePlanet;
public static String helloWorld() {
return "Hello World";
}
}
If we change the homePlanet value for one instance of Person, all of the other instances will have their values changed as well.
â Without the 'static' keyword, our methods and variables are instance variables and methods.
+ 10
Nazmul Islam Naim
I hope, this explanation will be useful to you! đ
+ 9
#1
I have created the Person class, and within that class we have an instance variable called 'personName' and an instance method called helloWorld().
Our variables and methods are called the "instance members" of the class.
public class Person {
private Name personName;
public String helloWorld() {
return "Hello World";
}
}
What is an "instance"?
An instance is a single occurrence of an object. While there is only one class, there are many instances of the class: objects.
âą For example, we can have hundreds and hundreds of different Person objects. Each Person object has its own instance of the personName object, each with its own value and its own version of the helloWorld() method. Besides instance variables and methods, there can also be static ones. All instances of a class "share" the static variables or static methods of the class.
. . .
+ 5
//Public keyword means that it can be accessed anywhere that is, within a class, another class,another assembly.
//Static variable and Methods can be used without having any instance of the Class. Only the Class is required to invoke a static Method or static variable.
//if you are looking for more info visit here
https://www.quora.com/What-is-difference-between-static-and-public-keyword-in-Java