#Should class variables be public or private?

1 messages · Page 1 of 1 (latest)

hollow forum
#

?

amber ospreyBOT
#

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

amber ospreyBOT
#

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.

hollow forum
#

I was wondering, should class varaible bet set to private, and I should put methods to access them?

flat pine
long hemlock
#

i assume it means any class field?

onyx relic
#

preferably private with setters/getters as that allows for greater flexibility and you can adhere to a design much better but it's up to you whatever you want to do, I've commonly done for some of my work ```java
public class Something {
public final int a;
public final int b;
public Something (int a, int b) {
this.a = a;
this.b = b;
}
...
}

#

Microsoft also follows a similar approach with some of their SDK's but it all depends what you're trying to achieve

#

static constants, often public

flat pine
onyx relic
#

Yep, my usecase is usually ala's former

flat pine
#

And by default you should learn to just use private with getters

#

in any case

#

for at least some time until you are intermediate

stuck palm
hollow forum
#

for a static variable

#

for example in the main

#

or would this be redundant and I should just go directly for the static variable instead?

flat pine
hollow forum
flat pine
#

depends then, usually you don't mind having public constants, and for non constants, they should be rare enough

stuck palm
# flat pine wdym

"never do that" confused me. what exactly is wrong with his way?
i started coding a few months ago so i am quite inexperienced and love for some practical insights

modern matrix
#

i guess it breaks the encapsulation(not good to know what you not supposed to)

molten arch
stuck palm
#

oh he was referring to static constants. i thought he was referring to his entire block of code. okay, yes thats clear to me. thanks!

flat pine
hollow forum
flat pine
hollow forum
#

ok so direct access inside the class

#

that was what I was wondering

#

thanks