#What is the need for changing a method or keywords from public to private??

1 messages · Page 1 of 1 (latest)

main kilnBOT
#

<@&987246399047479336> please have a look, thanks.

main kilnBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

pine sail
#

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

spice ferry
#

Other than restricting data access??

glad mirage
#

wdym by "data"

#

methods can be private too

#

public and private are access modifiers, that's what they are for

spice ferry
glad mirage
#

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

spice ferry
#

I can't control the private one?

crimson cairn
#

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

glad mirage
#

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)

nova kiln
#

okay so there are two mental models

#
  1. "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

#
  1. "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)

ancient yew
nova kiln
#

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?

tidal bridge
nova kiln
#

so long as you are allowed to by the module system

tidal bridge
#

oh get all methods ?

nova kiln
#

90% of the point of the module system is to prevent stuff like that being done to core libs

tidal bridge
#

or get all declared methods

nova kiln
#

there is a method called .setVisible

#

again, don't do it

tidal bridge
#

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

nova kiln
#

🤷‍♂️ idk what you are describing exactly

#

but reflection for mapping is pretty standard - just accessing private fields is a bad thing

tidal bridge
#

I was trying to create a DAO class and was mapping sql cell to a variable

nova kiln
#

you can reflect through constructors/setters

tidal bridge
#

nvm, I think I am only confusing 😓

silk epoch
#

You would always like to use lowest possible scope for design reasons.

#

To restrict other people from using stuff which they shouldn't use or don't need.

#

But in general private/public are visibility modifiers, so yes, their entire use is to manage visibility.

#

@spice ferry ^