8

Normally when creating new global variables I do not define its access modifier. So as per java it will adopt property default access modifier. When I'm need to access that variable in out of default scope I change its access modifier else leave it as it is. So my question is "Am I doing it right way? Is it normal to have default access variables? or should I use private/public for them? Is it good coding practice to not to use access modifiers?"

Harry Joy
  • 2,197
  • 2
  • 18
  • 20

3 Answers3

13

2 things here:

  1. don't use globals :)
  2. it's common/best practice to assume all fields are private unless there's an overriding reason to make them anything else, in which case the most restrictive access should be chosen (usually protected).

Of course there's always exceptions to the rule, but that's the basics to start out from.

jwenting
  • 9,783
  • 3
  • 28
  • 45
  • Any specific reason for 1st thing? "Don't use globals". – Harry Joy Jul 04 '11 at 05:55
  • 12
    Globals can be accessed and modified from anywhere in the entire codebase. More than a handful of them is enough to explode a program's potential complexity beyond a sane person's mental capabilities. – tdammers Jul 04 '11 at 06:01
  • If they act like `static` in .NET, they also break unit testability; sincea unit that uses it will ALWAYS be dependent on them and there's no way to fake them to isolate the unit under test. – StuperUser Jul 04 '11 at 08:51
  • 1
    and of course they quickly become a maintenance nightmare. Had to make a "small" change once to an application riddled with hundreds of globals (a value had to allow a longer input, something like that). We ended up having to change not one or two but hundreds of sources, each in dozens of locations. – jwenting Jul 04 '11 at 09:50
  • 1
    Sorry to nit-pick, but in you're second point you suggest protected is the most restrictive access. The most restrictive access is default access modifier, not protected! – oOTesterOo Jun 12 '14 at 16:17
5

A few general programming principles here:

  • Simple is better
  • Don't state the obvious

...but also:

  • Explicit is better than implicit

I'm not a Java expert, but the general rule of thumb should be: If what happens is obvious to someone familiar with the language without being explicit, use the implicit behavior. If however this might put someone on the wrong track, then by all means be explicit.

tdammers
  • 52,406
  • 14
  • 106
  • 154
  • 3
    In Java default access is a separate kind of accesss, so if you add an access modifier, you don't restate the accesss, you change it to something else. That's why if you really need explicit default access, the only way to make it explicit is to use a comment. – Malcolm Jan 26 '12 at 07:32
-1

Java doesn't have automatic properties, so all class member(s) in general should be private with get/set methods for public usage, if required.

According to the source (Oracle java Docs) this is the recommended approach.

"Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to. Avoid public fields except for constants. Public fields tend to link you to a particular implementation and limit your flexibility in changing your code. "

http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Nickz
  • 1,430
  • 1
  • 13
  • 18
  • 1
    But, what if you want to share the access with only *some* things and not **everything** (as `public` does)? –  Jul 07 '14 at 17:02
  • If you are going to slap a (public) getter and setter on to a private field, you might as well make the field public. – Utku Apr 04 '21 at 22:20