Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 13 down vote accepted

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.

share|improve this answer
    
Any specific reason for 1st thing? "Don't use globals". –  Harry Joy Jul 4 '11 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 '11 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 '11 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 '11 at 9:50
    
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 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.

share|improve this answer
2  
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 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

share|improve this answer
1  
But, what if you want to share the access with only some things and not everything (as public does)? –  MichaelT Jul 7 at 17:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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