A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.

"Greetings, Amigo! I hear you already have a good grasp of methods?"

"Hi, Rishi. Yes, I've already cut my way through that teacher's trick. I would say that it wasn't so bad, but you'll tell me, 'No, no! You haven't figured out anything.'"

"You've clearly been spending too much time chatting with certain teachers, probably with Diego. Anyway... I still hope you understand methods well enough. After all, today I'm going to teach you some magic words that help delineate methods' spheres of influence."

"Sounds intriguing."

"In fact, it's all simple. Before each method, programmers can specify so-called access modifiers. These include the following keywords: public, protected, private.

"These access modifiers let you restrict other classes' access to a method.

"For example, if you write the private keyword before a method declaration, then the method can only be called from the same class in which it is declared. The public keyword allows access to the marked method from any method of any class.

There are a total of 3 such modifiers , but there are 4 types of access to a method. This is because the absence of an access modifier also means something. Here's a complete table:

Access from...
Modifiers Any class Child class Its package Its class
public Yes Yes Yes Yes
protected No Yes Yes Yes
no modifier No No Yes Yes
private No No No Yes

"And here is a complete explanation of the access modifiers:

1. public modifier

A method (or variable, or class) marked with the public modifier can be accessed from anywhere in the program. This is the highest degree of openness — there are no restrictions.

2. private modifier

A method (or variable, or class) marked with the privatemodifier can only be accessed from the same class where it is declared. For all other classes, the marked method (or variable) is invisible. It's as if it does not exist. This is the highest level of restriction — only its own class.

3. No modifier (default modifier)

If a method (or variable) is not marked with any modifier, then it is considered to have the 'default modifier'. Variables or methods with that modifier (i.e. with none at all) are visible to all classes in the package in which they are declared. And only to them. This modifier is also sometimes called package-private, hinting that access to variables and methods is open to the entire package in which their class is located.

4. protected modifier

If a method is marked with the protected modifier, then it can be accessed from the same class, the same package, and descendants (classes that inherit the class in which the method is declared). We will analyze this topic in more detail in the Java Core quest."

"Interesting, but I'm not sure whether I can immediately put these modifiers in all the right places.

"You'll get there gradually. No need to worry ahead of time. Until you reach the end of the Java Syntax quest, you can use the public modifier on all your methods (as well as classes and instance variables). You will need the other modifiers when we start actively learning OOP."

"Can you explain in more detail why access modifiers are needed?"

"They become necessary for large projects written by tens and hundreds of programmers at the same time.

"Sometimes there are situations when a programmer wants to split an excessively large method into parts and move part of the code into helper methods. But at the same time, he or she doesn't want other programmers to call these helper methods, because the corresponding code may not work correctly."

"So they came up with these access modifiers. If you mark a helper method with the word private, then no code other than your class can see your helper method."

"I think I understand."

New Java Syntax, level 7, lesson 5
The struggle for access
Before you is a program that displays information about a person. Unfortunately, it doesn't compile. Change the minimum required number of access modifiers in the Person class for the code to compile.

static keyword

"There is another interesting keyword. It is static. Unsurprisingly, it makes methods static."

"What does that mean?"

"I'll tell you more about it later. Don't worry. For now, just try to remember a couple of facts about static methods.

Fact 1. A static method is not attached to any object, but instead belongs to the class in which it is declared. To call a static method, you need to write:


Examples of static methods:

Class name Static method name
Thread.sleep() Thread sleep()
Math.abs() Math abs()
Arrays.sort() Arrays sort()

The class name before the name of a static method can be omitted if you call the static method from within its class. This is why you don't need to write Solution before the names of each of the static methods that are called.

Fact 2. A static method cannot access the non-static methods of its own class. A static method can only access static methods. As a result, we declare all the methods that we want to call from the main method static."

"Why is that?"

"You will answer this question yourself when you start learning OOP and understand how static methods work. Until then, just trust me.

New Java Syntax, level 7, lesson 5
Analyzing arrays
This program should display information about the created array. But due to misplaced static modifiers, it won't compile. Correct these mistakes. Add the static modifier where it is needed.

throws keyword

"There is another keyword that you've probably seen in a method declaration — the throws keyword. Unlike access modifiers and the static keyword, this keyword is placed after the method parameters:

public static Type name(parameters) throws Exception
  method body

"And what does it mean?"

"Once again I have to tell you that you will learn its true purpose later, when we study exceptions (in Level 15).

But to touch on it superficially, we can say that a method marked with the throws keyword can throw errors (exceptions), meaning instances of the Exception class (and classes that inherit it). If several different types of errors can occur in a class, then you need to list each of them separated by commas."

"Sounds mysterious and incomprehensible! I'll have to wait for Level 14."

main method

"Let's now take a closer look at the main method. You already understand that the line where a method is declared, which contains all the modifiers, will affect how this method is called from other classes and methods. Additionally, it affects the type of result the method will return and indicates what errors are possible as it runs.

"Such a line is called a method declaration and has the following general format:

access modifier static Type name(parameters) throws exceptions
General format of a method declaration

Where access modifiers is replaced by public, protected, private, or nothing at all;

if the method is static, then the static keyword appears (it is absent for non-static methods)

Type is the type of the return value (void if there is no result)

"Now you have a much better understanding of what all these words mean in the declaration of the main method:

public static void main(String[] args) throws Exception
Declaring the main method

"Well, now I realize that access to the main() method is possible from any class, as indicated by the public keyword. The method is static, so it can be called explicitly as Solution.main()."

"What result does the main() method return?"

"None at all! The result type is void. It's kind of like an empty type, a placeholder."

"What does main() have in its parentheses?"

"Hmm... It turns out that the main method takes arguments (!). They are passed in as an array of strings."

"That's right. And the parameter name args suggests 'arguments' to our minds. When the program starts, you can pass it arguments — an array of strings. They will be contained in the args array in the main() method."

"Whoa! I wondered about this when I saw it the first time, but then I got used to it and started writing the parameter list without thinking."

"We've all been there. Finally, unhandled errors like Exception (or its descendants) can occur in the main() method. We know this thanks to the throws Exception part of the declaration."

"Thank you, Rishi. I didn't understand everything, but this was interesting."

"You are welcome. Gradually you'll come to understand all these subtle points, I'm sure."