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
2 things here:
- don't use globals :)
- 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.
-
Any specific reason for 1st thing? "Don't use globals".Harry Joy– Harry Joy2011年07月04日 05:55:00 +00:00Commented Jul 4, 2011 at 5:55
-
13Globals 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– tdammers2011年07月04日 06:01:05 +00:00Commented 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– StuperUser2011年07月04日 08:51:06 +00:00Commented Jul 4, 2011 at 8:51 -
1and 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– jwenting2011年07月04日 09:50:24 +00:00Commented Jul 4, 2011 at 9:50
-
1Sorry 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– oOTesterOo2014年06月12日 16:17:54 +00:00Commented Jun 12, 2014 at 16:17
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.
-
3In 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– Malcolm2012年01月26日 07:32:47 +00:00Commented Jan 26, 2012 at 7:32
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
-
1But, what if you want to share the access with only some things and not everything (as
public
does)?user40980– user409802014年07月07日 17:02:46 +00:00Commented 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– Utku2021年04月04日 22:20:35 +00:00Commented Apr 4, 2021 at 22:20