java - Why do we make private fields instead of package local -


  1. is there overhead when using default access level on class field in java? mean "any", nanoseconds on startup. heard jvm makes graph of scopes fields, possible reason overhead.

  2. i'm lazy write private keyword. there reason write private keyword instead of package-local? package local seems local enough.

"is there reason write private"?

yes there reason. not want private members accessed.

see site summary of access modifiers , when , why should use them, http://docs.oracle.com/javase/tutorial/java/javaoo/accesscontrol.html

  • private = not okay them changed other mother class.

  • no modifier = okay them changed class within same package.

  • protected = same no modifier + okay changed subclass

  • public = okay them changed in general.

if clicked link above heed warning recommendation

use restrictive access level makes sense particular member. use private unless have reason not to.

if put no modifier saying,

"i explicitly want other programmers change these fields if class in same package mine."

you should always put private unless otherwise , doing saying,

"i not want other programmers change these fields."


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -