This question already has answers here:
Simple way to understand Encapsulation and Abstraction
(16 answers)
Java Encapsulation Concept not clear
(12 answers)
Closed 4 years ago.
Concept of encapsulation:If we can change the value of private variable indirectly(through setters and getters), then how is the private variable secured, how is it hidden?Could someone please explain in detail..
You're correct. If a field is exposed via getters and setters, it is not encapsulated. It's annoying how many people don't understand this so it's not surprising that you're confused.
Check out this answer. Essentially, using the keyword private we are able to prevent outside use of the fields in a class, for example. It hides the data in a class associated with that private keyword.
Related
This question already has answers here:
When is using public fields acceptable?
(3 answers)
Closed 5 years ago.
Java has different access levels for fields:
public
protected
default
private
Due to encapsulation concept we always trying to use private modifier with getters and setters of needed access level. It has various advantages shown in this answer. Since getters and setters are so cool, when should we prefer public/protected/default Object field; to private Object field; with public/protected/default getters and setters?
Clarification: I perfectly understand why and what are getters, setters and access modifiers. I just want comprehensive answer for a certain question above.
We can achieve complete encapsulation in java by making members of a class private and access them outside the class only through getters and setters. Although a lesser degree of encapsulation can be achieved by making the members public or protected.
This question already has answers here:
What is a JavaBean exactly?
(23 answers)
Closed 6 years ago.
In bean standards, it is mentioned that it must implement serializable.
But in general developers say to create beans which means simple setter/getter.
Is bean correct terminology or any other terminology for class with setter and getter methods?
I think it's just called a class that follows Object Oriented Programming (OOP) principles when you use getters and setters and make the fields private.
http://codebetter.com/raymondlewallen/2005/07/19/4-major-principles-of-object-oriented-programming/
This question already has answers here:
Private enum constructor
(8 answers)
Closed 6 years ago.
I believe enum constructors in Java are meant to be private but I've recently switched over to IntelliJ and it says that the private keyword is redundant when I add it to the constructor.
Picture:
http://puu.sh/nYbwp/1c37312777.png
Does Java just automatically make them private or something?
yes. Enum constructor is private by design.
And it is not the only example of access modifier's redundancy in Java - all interface methods are public and public modifier for them is not necessary too
This question already has answers here:
Java -- private constructor vs final and more
(3 answers)
Closed 7 years ago.
As per my understanding, a class is declared as final to prevent it from being extended/inherited. So I see there can be security and probably some performance gains in this regard.
But is there a very specific design decision behind this? Say for eg: to realize some kind of design pattern? I did go around a similar thread here! but the answer was not really what I was looking for
Singleton Pattern:
-Private Constructor
-Only static methods
-No need to have more than one object of this class or an object at all
-No need to extend this fundamental class
This question already has answers here:
Difference between static class and singleton pattern?
(41 answers)
Closed 9 years ago.
Having read Difference between static class and singleton pattern?, none of the answers list any advantages for using a static method over a singleton, which leads me to wonder why anyone would ever want to use static methods.
As with all questions of this nature, use the right tool for the job. Use a singleton when your class represents an object that there can be only one of. Use static methods when your methods are appropriate for the class they are members of but do not rely on a specific instance of that class.
In general, use your best judgment. Go for clean, precise, maintainable code, keeping the overall big picture in mind.