Access Modifiers 3589 - Chuck Norris Cipher Encoder (Java)
Access Modifiers 3589 - Chuck Norris Cipher Encoder (Java)
Access Modifiers 3589 - Chuck Norris Cipher Encoder (Java)
Access modifiers.
Theory. 12 minutes reading.
You've probably seen the following piece of code: public static void main(String[]
args); some code goes here.
Why is the word "public" even there? As you can probably guess, it means that the
main(...) method is available to everyone. The word public here is an access
modifier.
An access modifier is a special keyword that specifies who is allowed to use your
code or a special part of it. It can be placed in front of any field, method or the
entire class.
So, you already know at least one of the access modifiers! The others are: package-
private, protected, and private. Why do we need them? Let's find out.
3. Private members.
A class member (a field or a method, e.g. class constructor) has more options to
choose from: private, package-private, protected and public. As you can see, there
are two additional modifiers here. Let's consider member modifiers in more detail.
Fields are often declared private to control access to them from any other class.
In some cases, these fields are only used internally in the class and there is no
way to change and even access them from any other class. In other cases, it can be
done via accessor methods (e.g. getters and setters). Getter and setter methods are
used to protect and hide your data when creating classes. A getter method returns
the value of a field, while a setter method sets or updates the value. We will
discuss the main features of getter and setter methods later in the topic related
to these terms.
Private methods are used to hide the internal low-level logic implementation from
the rest of the code and make public methods more brief and readable.
Here is the class Counter with the private field current. This field can be read
with the method getCurrent() , a getter method, and changed with the inc() method.
The last one is not exactly a setter method because it doesn't manually set a value
to a current variable, but just increments it.
public class Counter.
private long current = 0;
public long getCurrent().
return this.current.
public void inc().
inc(1L).
private void inc(long val) {
this.current += val.
Sometimes, a private class constructor is required. This type of constructor can
only be used inside the class, e.g. from another constructor, which can be public
or private too, or from the class methods.
4. Package-private members.
A package-private access modifier does not require any keyword. If a field, a
method, or a constructor has this modifier, then it can be read or changed from any
class inside the same package.
Let's see an example. Here are two classes in the same package: Salary and
Promotion.
The class Salary has a package-private field and a constructor. An instance of the
Salary class can be created inside a Promotion class, and the field can also be
accessed by Promotion and its members because they belong to the same package.
public class Salary.
long income.
Salary(long income).
this.income = income.
public class Promotion.
Salary, salary.
Promotion(Salary, salary).
this.salary = salary.
public void promote().
salary.income += 1500.
6. Conclusion.
In this topic, you learned about access modifiers that allow you to determine who
will be able to use the code. Using them makes your code safer and clearer. In
conclusion, here is a piece of advice: use the most restrictive access level that
makes sense for a particular member. Don't make all members public.