CLASSES IN JAVA
VARIABLES AND SCOPES
As we already know from looking at the image below, x is an instance attribute of the MyClass class. In this section we will discuss variables and scope by meaning the scope of visibility of variables.
x looking at the figure further up, it is the first type of variables that we can go and define. It is a kind of global variable (visible throughout the class). This attribute is visible within the body of code or body that represents the class. The scope for using a more appropriate definition is the code block of the class. We now insert an instance method that performs the difference between two variables passed in as input as parameters. The scope in this case of the variables a and b (parameters passed to the method) is the code block of the instance method sott. Outside the method these two variables are not visible. This is the second type of scope that we can define in Java. a and b are local variables.
We now introduce a new variable k. Variables that we introduce within a method are also local. This variable is visible both in the block of code in which it is declared and in any nested sub-blocks; the scope starts from the point at which it is defined, not before.
We try to define a variable with the same name k in the nested block. We cannot define in a nested block a variable having the same name as a variable defined outside the block, this is because the outermost k variable is still in scope, that is, it has not left its scope of visibility.
Let us now try changing the name of the variable k to x, x being the instance variable having the entire class as its scope. In jargon we say that we are hiding the instance variable; in fact, in the instance method, x represents the local variable and not the instance variable. We can actually use the instance variable with a mechanism that we will now see.
THE KEYWORD THIS
Inside a method allows us to refer to the instance we are using. this refers to the current object. Whenever a method is invoked, in addition to the normal arguments behind the, so, an additional argument is passed that is called this. this is an implicit reference on the instance on which we are invoking the method, translated means this and stands for “This instance.” Let’s look at an example.
What we can do with the this keyword is this. We can use it as a normal reference to the instance of the class on which we are invoking the setAttr method.
Let’s look at a use case. Sometimes it is necessary to call parameters such as instance attributes, since very often attributes are valued through the parameters of a method such as setAttr. But we have a problem that the figure highlights. x and y within the method refer to the two parameters, the instance attributes were hidden by the local variables .
Now we are going to rewrite the code as shown in the figure. Now the math adds up because by this.x and this.y we are referring to the instance attributes, correctly valued by the x and y parameters. Also in viewAttr() we can use this.x and this.y to refer to the instance attributes.
PARAMETERS AND ARGUMENTS
Parameters are the values that methods can ask for as input when they are invoked. Let’s look again at the ContoCorrente class and insert a new method. Is a transfer of money from one account to another, in the figure we see that first a withdrawal is made from the account, then on the instance of the ContoCorrente passed in input a credit is made equal to the value of the second parameter amount.
We have seen that the definition of a method may involve the use of parameters; at the time a method is invoked, the values passed in are called arguments. When defining a method we can include a parameter definition, when it is invoked the values passed are called arguments. This is the difference between parameters and arguments.
PASS AN OBJECT TO A METHOD
Let us go back to the previous code and try to understand what is the difference of when we pass as an argument to a method a reference type versus a primitive type.
value already has its own associated memory and contains the amount 500.00.
The value 500.00 is passed to the method. The passing of the primitive type was performed by-value, the argument value was duplicated and passed to the parameter. These two variables are completely disconnected from each other.
If we change the amount locally the value variable retains its value.
In reference types, things change. The argument that is passed to the method is an instance of the class ContoCorrente. We know that in a type reference cc2 contains a reference (address) to the memory area where the object is located.
The passing of cc2 to the parameter is done in the same way as the primitive types for copying, with one very important difference, the value that is duplicated and passed By-Value is not the ContoCorrente object but its reference. The invocation of the deposit method occurs on a copy of the reference, that is, on the same object pointed to by cc2.
At the time when the execution of the transfer method ends and the dest variable exits scope, the balance value remains correctly at 3000.00.
RETURN AN OBJECT FROM A METHOD
Now we will learn what happens when an object is returned by a method. We introduce the class. Whenever we invoke the static create method we get as the return value from the method an instance of the MyClass class.
Let’s see what the view() method displays.
mc exits scope at the end of the method however the created object survives as it is assigned to another reference in the variable that acquires it as a return value(mc1).
LINKS TO PREVIOUS POSTS
LINK TO CODE ON GITHUB
EXECUTION OF THE EXAMPLE CODE
- Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.
java -jar –enable-preview CourseJava.jar
- Or run the main found in the file CorsoJava.java.
Leave A Comment