In this course, we'll learn the object-oriented concept in Java.
- Object-Oriented programming concept
- Object & Class
- Access Modifiers
- Naming Conventions
- Static Keyword
- Nested and Inner Classes
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we are going to learn more about the Scope and what are actually the code blocks. If you remember, in the previous lessons, we touched on the expression, statement, and block concepts. But since we are at the beginning of the course, we did not go into too much detail. In this lesson, we'll talk in more detail about the concepts of scope in Java. In the previous videos, we have seen that local variables are variables declared in the method. Scope means just a range at which something is available. In other words, it just means where the data exists. For example, local variables are available only in that method. That is why they are called local. And variable in code blocks is just available to that code block.
The concept we call scope in Java is actually code blocks whose boundaries are set with curly braces. So, every scope starts with a curly brace and ends with a brace again. If there are curly braces at the beginning of the code block, a new scope has started. For this reason, that scope should also have a closing curly bracket at the end of it. If there is only one opening curly brace or closing curly brace, the compiler will throw an error. A block that is opened with a curly bracket should be enclosed with another curly brace. Also, if you encounter any curly braces in Java, it means you have entered a new code block. Each block of code has its own scope. When there are multiple blocks, you must match them from inside to out.
So, how is the access to variables in code blocks? You can access variables you created in the outer scope from the inner scope, but you cannot access a variable you created in the inner scope from the outer scope. So, if you are in the inner block there is access to the outer ones. If you were in the outer block, there is no access to the inner ones. If we look at the example on the slide, we can access the age variable defined in the outer scope from the inner scope 2, and we can also assign a value to it or change its value. But we cannot access the variable age2 which is defined in the inner scope from the outer scope. In this case, we get a compilation error. Now, let's move on to eclipse and get some practice. First, I will create a new class. So, I will right click on the source folder in the object oriented concept project, select new and class options.
The package name can be scope and the class name can be ScopeExamples. As you can see, currently there is one class named "ScopeExamples" and a method named "main", and both of them have their own scope. The scope of class begins with this curly brace and ends with this curly brace. And inside the scope of the class, there is the "main" method. The "main" method begins with this curly brace and ends with this curly brace. Now, let's make some practice. First, I will create a new variable in the scope of the class. String name "David". Now let's try to access this variable in the main method. I will write name here, but we cannot access this variable. Normally, the variable name is defined in the outer scope and we need to be able to access it because the main method is the inner scope. This is because of the static keyword, not the scope.
If you remember, we could only use static variables or methods directly inside a static method. Otherwise, we had to access it by creating an object from the relevant class. Since the main method is static, to use the name variable in this static main method directly, we also have to declare this variable as static. Pay attention to this. I wanted to remind you once again because it is important. Now, let's define the variable "name" as static. I write the keyword static before the word "String". Yes. As you can see, we can now access the variable named "name" in the main method. We can change its value here if we want. For example, let name be "Ronaldo" this time. Now, let's create a different variable in the main method, int age = 20. Now let's try to access the age variable from within the class.
If you notice, we cannot access the age variable from here because the age variable is defined in the inner scope and there is no access from the outer scope. If you want, write this before the beginning of the main method. Still, the situation does not change. What matters is whether the variable is defined in the inner scope or the outer scope. Now, let's define a new method outside the main method, but inside the ScopeExamples class, public static void myScope(). In this method, first, let's try to access the name and age variables respectively. As you can see, we can access the name variable because the name is in the outer scope and can be accessed from the inner scope. Now let's access the age variable, but if you notice, we cannot access the age variable because the variable age is in the scope of the main method, but we are currently in the scope of a different method.
Therefore, there is no hierarchy between these methods and they are independent of each other. Now let's create a new scope with curly braces inside the main method. We can access the name and age variables from inside the scope because we can access the variables in the outer scope from the inner scope. Now, let's define a variable with the same name, int age = 30. Notice that we get a compilation error. Because we already have access to the variable from this scope. So, it doesn't allow us to create a new variable with the same name. So, let's make it age2. Now, let's try the opposite. So, let's create a variable named age2 in the main method. If we create it right here before the inner scope, we'll get the same error again because the code flow is from top to bottom.
And if we create the age variable 2 in this field, then we need to remove the int data type in the inner scope or change the variable name. Therefore, let's create a variable with the same name after the inner scope ends, int age2 = 35. As you can see, this is allowed because the program will not give an error when it comes to the inner scope during the compilation phase since there is no age2 variable. And when it comes to this line, it will be compiled again without any error since it's no longer from the inner scope. In addition, you can see multiple nested scopes, and we don't need to define any variable in every scope. For example, let's create two more nested scopes here.
But let's just define a variable in the innermost scope, int age3 = 40. We can also access the outermost variables, such as name or age or age2 within this scope. Of course, these scopes may not always appear on such a regular basis. For example, let's delete these three closing parentheses and let's add three closing parentheses at the end of the line where we defined the age3 variable. This is also valid, but it's a very complex and difficult-to-understand syntax. The important thing here is that there is a closing parentheses for each opening parentheses.
Now, let's refactor this with automatic code formatting. If you select 'Source' from the top menu and then click the 'Format' option, your codes will be automatically formatted. Or you can use the control + shift and F keys as a shortcut. As you can see, the scopes have been rearranged. Yes. I think the concept of scope is understood. In conclusion, the thing to keep in mind is that each curly brace represents a scope. And you can access variables defined in the outer scope from the inner scopes, but you cannot access variables defined in the inner scopes from the outer scopes. Let's take a short break here. I'll see you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.