Is it ever acceptable to make instance variables public? If so, when?
SMITA6076
0
Light Poster
Recommended Answers
Jump to PostIt's a best practice to make all instance variables private (or protected at least). In this way, you are enforcing the concept of encapsulation.
The only time I use public instance variables public is when they are constants, such as
public static final String ACCEPTABLE_PUBLIC = …
Jump to PostIt's certainly acceptable and even desirable if not required in some specific cases.
If and when you run into such cases, you'll know them as there will be no way to get the application compiling and/or running when doing things in other ways.
All 8 Replies
dononelson
2
Junior Poster in Training
jon.kiparsky
commented:
Good answer
+2
jwenting
1,889
duckman
Team Colleague
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
jwenting
1,889
duckman
Team Colleague
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
jwenting
1,889
duckman
Team Colleague
jon.kiparsky
326
Posting Virtuoso
VernonDozier
2,218
Posting Expert
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.