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?"

3 Answers 3

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.

5
  • Any specific reason for 1st thing? "Don't use globals".
    – Harry Joy
    Jul 4, 2011 at 5: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 4, 2011 at 6: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 4, 2011 at 8: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 4, 2011 at 9: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, 2014 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.

1
  • 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, 2012 at 7: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

2
  • 1
    But, what if you want to share the access with only some things and not everything (as public does)?
    – user40980
    Jul 7, 2014 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 4, 2021 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.