#What is the need for changing a method or keywords from public to private??
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
To limit access to what classes can call a method/field
e.g. let's say you have a Logger. You don't want people who have access to your object to also be able to access a class specific logger
Anything else??
Other than restricting data access??
wdym by "data"
methods can be private too
public and private are access modifiers, that's what they are for
I meant other than hiding things.
nothing
it's for hiding things
for example, you can have a public facing method that does input validation and a private one which does not
because you can control it when it will be called and therefore can ensure that only correct inputs will be passed in
Didn't get the "control" part.
I can't control the private one?
only from inside the class, not from outside
he means if u got for example 2 methods, 1 public and 1 private, that u can only call the public from other places, not the private one
If you are developing a class and mark one of the methods as private, it means that it can only be called within that class, which means only you can call it (since you're the one who writes that class)
okay so there are two mental models
- "everyone is malicious"
in this mental model you are working with the assumption that what you are writing is a library to be consumed by some manner of "external" folk
that "external" folk perhaps being you in the future
and you make things private so that they don't muck up the works
- "proper design"
in this mental model you only expose the minimum surface area to a class because that makes it easier to do stuff like refactor as well as understand how the code interacts with the rest of your codebase
there are no malicious/stupid actors, but if something is private you don't have to think about access of it outside of the file
this has benefits to your, and future you's ability to reason locally about code
now none of this applies to if you were taught to do this
public class Thing {
public String name;
}
public class Thing {
private String name;
public String getName() {
return this.name;
}
public String setName(String name) {
this.name = name;
}
}
at least not directly and immediately
there are reasons to prefer accessor methods over straight fields
under mental model 1, you can do stuff like copy a list before handing it out
private List<String> things;
public List<String> getThings() {
return new ArrayList<>(this.things);
}
to prevent folks mucking about
but also under mental model 2 it can help you reason about code since you know after .getThings() you couldn't have called .add and altered the object (i.e. it increased local reasoning)
private has nothing to do with security, since you can bypass it
security i'm using loosely
not in terms of an actual "external actor"
but in terms of "dev on another team" or "me after too much egg nog"
you can also use accessor methods directly as lambdas
so if you have
public class Thing {
public String name;
}
you can sort by name with
List<Thing> things = ...;
things.sort(Comparator.comparing(thing -> thing.name));
but if you have an accessor method it feels nicer
public class Thing {
private String name;
public String name() {
return this.name;
}
}
List<Thing> things = ...;
things.sort(Comparator.comparing(Thing::name));
both of the above reasons are why java records expose accessor methods and not the fields directly
record Thing(String name) {}
// name is implicitly private and final, exposed by an accessor method
once you leave the realm of "just data" then the benefits become more apparent under model 2
since stuff like sockets
they hold some non trivial internal state
which could be massively different depending on a number of factors
in fact, sockets were recently totally re-implemented behind the scenes
if their fields were not private then there would be no "behind the scenes", we would have been stuck with whatever the 1.0 implementation was forever
private matters a lot more in cases like that - and in libraries used by a ton of people in particular
@spice ferry does that track?
wait how to bypass it, suppose I am importing a package and a class has a private function, how can I access it 
technically you can do it via reflection
so long as you are allowed to by the module system
oh get all methods ?
90% of the point of the module system is to prevent stuff like that being done to core libs
or get all declared methods
I see, I used it recently but lint showed me its a bad technique
like I needed to map column name say "size" to column var size, not sure what would have been the best way
the class had like 100s of these column names to be mapped to var, so I used reflection to get all the names and check if it matches with current db column, if so then use it
🤷♂️ idk what you are describing exactly
but reflection for mapping is pretty standard - just accessing private fields is a bad thing
I was trying to create a DAO class and was mapping sql cell to a variable
you can reflect through constructors/setters
nvm, I think I am only confusing 😓