What is the difference between static and public in java ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the difference between static and public in java ??

7th Aug 2020, 8:02 PM
Ziad Emad
Ziad Emad - avatar
2 Answers
+ 5
They are completely different. public is an access modifier that indicates that a function or variable can be accessed outside the class. static is a non-access modifier that indicates that a function or variable belongs to the class and not the object. Example: public class Foo { public int a = 9; public static int b = 10; } Foo bar = new Foo(); bar.a; //Works because a is public Foo.a; //Does not work. a belongs to the object not the class Foo.b; //Works because b is static
7th Aug 2020, 8:06 PM
Ore
Ore - avatar
+ 1
Thanks 😊
7th Aug 2020, 10:07 PM
Ziad Emad
Ziad Emad - avatar