About Static and Public Key word | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

About Static and Public Key word

Where is the difference between "static" And "public" key word?

20th Feb 2019, 1:50 PM
Nazmul Islam Naim
Nazmul Islam Naim - avatar
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.
20th Feb 2019, 3:46 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 10
Nazmul Islam Naim I hope, this explanation will be useful to you! 😊
20th Feb 2019, 3:48 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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. . . .
20th Feb 2019, 3:41 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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
20th Feb 2019, 2:47 PM
Sudarshan Rai
Sudarshan Rai - avatar