ACCESS MODIFIERS - JAVA: Learn JAVA Quickly (2017)

JAVA: Learn JAVA Quickly (2017)

CHAPTER 6. ACCESS MODIFIERS

public – visible for all classes

protected – visible within package and subclasses.

private – visible only within class

default (friendly) – when there is no modifier, it is treated as default and visible within package

From this we can conclude that our method, for example, that is public is accessible in whole project. But what is with other modifiers? How to handle them? Now we will see one example with private modifier.

First add two classes PrivateClass and TestClass (with main method). In first class we are creating one simple method that will be called:

privatevoidmethodOne() {

System.out.println("Method is called!");

}

In second class to call this method we first need object. In this case we will create object of PrivateClass class, just like this:

PrivateClass pc = newPrivateClass();

pc.methodOne();

It complains: The method methodOne() from the type PrivateClass is not visible

This is just what we said. Our method is private, so it is visible(accessible) only within class. We can`t reach that method from another class. To solve this click on error and change visibility of ‘methodOne()’ to ‘package’. Our method should now looks like this:

void methodOne() {

System.out.println("Method is called!");

}

This is default modifier. What will happen if we change modifier to public?

publicvoid methodOne() {

System.out.println("Method is called!");

}

Nothing. Public methods are accessible from whole out project. Change now modifier to protected.

protectedvoid methodOne() {

System.out.println("Method is called!");

}

Same again. Protected method is accessible within package.

THIS

In one of our previous section, Constructors, if you remember this keyword was used. We had a few attributes like name, age and city and based on them constructor of class Professor with input parameters was constructed.

this.name = n;this is a reference to the current object — the object whose method or constructor is being called. So this refers to name attribute (attributes name, age and city belong to Professor prof = newProfessor();, static fields belong to class what we will discuss). Parameters in constructor String n, inta, String creserve some memory space and they need to be exactly type like attributes (String, int).

Primitive types are not only that could be parameters in constructors or methods. It is also possible to place object as parameter. We need class Person and TestPerson (with main). In our first class we will create constructor with parameters, attributes of person and method which will print our attributes. Let`s see example:

String name;

intage;

Person(String n, inta) {

this.name = n;

this.age = a;

}

void printMyName(Person p) {

String myNameAge = p.name + " " + p.age;

System.out.println("My name and age are: " + myNameAge);

}

As it is mentioned, object is passed as argument. Our method refers to name and age attributes using p reference of Person object. myNameAge is String whose stores that value and later on is printed to console.

Person person = newPerson("Name", 27);

person.printMyName(person);

expected: My name and age are: Name 27

To call our method we need object with parameters just like in Person class. This code is supposed to be inside TestPerson class (in main method).