public static everything in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

public static everything in java

Hello World! I have a simple question but I can't understand the reason why it happens? So why I shouldn't create every variable and every method as public static to have access to them from everywhere?

18th May 2019, 10:28 AM
Lighton
Lighton - avatar
2 Answers
0
Because sometimes you only need the variable in a part of the code, and it is simpler, more readable to not make it static and public. For example: for (int i = 0; i < x; i++) { a += i; } For this, the variable i is useless after the loop, so you don't have to make it public and static
18th May 2019, 10:30 AM
Airree
Airree - avatar
0
So what if I make it public and static? Will it run slower or will take more memory?
18th Jun 2019, 1:15 PM
Lighton
Lighton - avatar