Apps development for Android step by step

Lesson 0.12: Java basics – more about methods & variables (access modifiers public or private, getters and setters)

In this lesson we are going to understand what happens, if there are a few variables with the same name. And what does it mean that some variables or methods are public and others private? We would also learn how to easily add so called getters and setters. 

It can be helpful to read previous lessons about Java main concepts before proceeding with this lesson.

Instance variables – unique data for every copy of an object

Let’s go back to our Book object discussed in previous lessons. In the class Book we declared two variables: first holding a title, second storing number of pages.

Values of those variables varies from book to book (or from one object instance to another one). This is obvious that every book could have a different title or number of pages. Those variables are called instance variables (or instance fields). They have unique values for every instance.

Static variables – one for all instances

Can we imagine an opposite situation that we need only one variable for a class, independent from specific instances? A total number of books is a good example. This should be a common value for all instances. To mark that a variable is the same for every instance we use keyword static. Static variables are also called class variables.

This is a static variable declaration:

Now let’s look what would happen in the following situation:

Firstly we created two instances of Book class: bookOne and bookTwo. Then we assigned some values to variables title, numberOfPages and numberOfBooks. If we now ask for bookOne title and number of pages it would return “Title of Book One” and 120, but if we asks for bookOne number of books variable it would return 2, though we assigned to it 1. To make it more clear it’s advised to refer to static variables via class name not instance name as we did in the example.

So it should look like this:

Now it’s obvious that we modified the same variable and the current value is 2.

Constants – one value forever

Sometimes you need values that shouldn’t change over time. If values are constant you could write them into a code, but usually it’s better to use so called… constants. They behave as normal variables beside possibility to modify their initial values. To create constants we use a prefix “final”. There is a convention to use uppercase for constant names.

If we try to change it, we would notice an error.

Compilation of this code below (if we have declared BOOKSTORE as final in Book class) is impossible:

Usually we use static constants for a whole class.

Mind that the keyword final has also other applications in Java. You could declare final classes so it’s impossible to extend them (create subclasses) and declare final methods that cannot be overridden in subclasses.

Private or public – who could access our variables and methods

Now we would talk about so called access modifiers. We’ve already used keyword public in front of class names (for example: public class Book). Generally classes in Java are public so visible and accessible by other classes. You should keep public classes in separate Java files (read how to add a new class to project in previous lessons). There are some exceptions – it’s possible to create an inner class into another class, but we will learn about it in some future.

Though classes are public, variables and methods declared into classes could be private. So they are accessible only from inside a class.

Let’s add two more variables to our class Book: one public, one private.

We could easily access first one, for example:

But if we try to do the same with second one, we would see an error.

This won’t work:

Beside private and public there are two more access modifiers: empty one (you don’t use any modifier) and protected. We have already used variables declaration without any access modifier, for example int numberOfPages. This mean that such a variable is accessible by whole package – so all classes added to one package (in our test app a package is called com.example.myfirstandroidapp). Protected modifier behaves in the same way, but it allows access also by subclasses located in other packages.

This is a quick summary of available access modifiers:

Modifier keyword/Access level Specific class All classes in a package All classes in a package and all their subclasses in other packages All classes in the world
public yes yes yes yes
protected yes yes yes no
without modifier yes yes no no
private yes no no no


Remember that those access modifiers work not only for variables, but also for methods.

Access to private variables – getters and setters

You should always use the most restricted access level for variables that allows your Android app to work properly. So generally use private unless you really need more general one. We do it to be sure them some external code would not modify variable values without control.

This is a rule that you should obey. But if we created an instance bookOne of class Book and we would like to check its title, how we could do it? If we make it private, access from other classes would result in an error. To access private variables we should use methods!

Most common methods to access variables are so called getters and setters. They read (get) values of variables and write (set) new values to them. We could create them manually or use a function of Android Studio to automatically add them for us.

To add getters and setter follow these steps:

Step 1. Create or open a class with variables (in previous lesson you will find info how to do it).

For example:

Step 2. Click right button somewhere over a class code and choose from context menu: Refactor and then Encapsulate Fields. By the way: securing access to class variables only via methods is called encapsulation.

Encapsulation is one of the fundamental ideas of object oriented programming as it helps to control access to data (Android Studio)

Encapsulation is one of the fundamental ideas of object oriented programming as it helps to control access to data (Android Studio)

Step 3. You should see a window with encapsulation options. Just mark all your variables and click Refactor.

You could add getter and setter manually for every field (variable), but it’s much faster to use an option of Android Studio

You could add getter and setter manually for every field (variable), but it’s much faster to use an option of Android Studio

Just some comments. In the top you see a list of fields (variables) and also names for methods that would access them. Below (in Encapsulate section) you could decide to generate only getters or only setters and more importantly to add accessors (this is a general name of access methods so getters and setter) even for public variables. Finally we decide what would be visibility level of variables (we want private) and  visibility level of methods (default is public, but if we’re sure that our classes are going to be used only in this package as a part of small app, we could choose Package local instead).

Now the final code look like this:

Mind that getters have a type as they have to return a values and setters are void as they only copy parameter value to instance variable. We have already explained in previous lesson what keyword “this” does. It allows us to access instance variable if the local variable has the same name.

So now to set a title we have to write:

instead of

Local variables – live only in a method

Imagine that we have declared a variable with the same name in the class and in the method. It’s not an error. Moreover it happens really often.

Here is a code example:

What’s the value of numberOfPages: 100 or 200? Both… As they live independently. If we ask for the value inside a method countPages, the answer would be 200. But if we ask in the class or in other class methods the answer would be 100. Methods firstly use variables declared locally. Mind that values of local variables are kept only when method is executing. 

Remember that we could always refer in the method to instance variables using keyword this.

There is one important remark: you can’t use access modifiers to local variables. They live only in a method and could not be accessed from outside.

Summary: This was a long lesson… You should now understand level of variables (local, instance, class/static) and know to use final keyword. You should also see difference between four access modifiers (private, no modifier, protected, public). Moreover you should know why and how use getters and setters.

Knowledge gained from last three Java basic lessons should allow you to start using Java for Android app development.