Self-reference with this and How to invoking Classes and Objects methods

In the last tutorial related to Java Object Orientation, we talked about creating constructors, with or without parameters, in our classes. 

In this small class, we will learn how to reference members of an object through the keyword 'this', which is a tool widely used by programmers in Java methods within classes. 



Referencing members of the class with this

Imagine that we create a class called "Employee", where its default constructor receives a string with the name of the employee, an integer to identification number and a double with his salary. 
If these data are within the class: 

 private String name; 
 private int ID; 
 private double salary; 

And the header of the constructor is: 
public Employee (String name, int id, double salary) 

How do we make the assignment? Now, the same way we did before: 
name = name; 
ID = ID; 
salary = salary; 

Oops! He noted that the class variables and variables header has the same name? 
And now, like Java will know that the variables on the left refer to the variables 'private' class to the right are sent to the user creating the object? 

I know! Simply create the method with different variable names, as we did before: 
public Employee (String Name, int id, double salary) { 
 name = Name; 
 ID = id; 
 Salary = salary; 
} 

Ok, this would work perfectly. But it would be extremely uncomfortable and disorganized, create two names for the same type of variable. 
Can not seem to matter now, with this simple application. But in a real situation, in which your Java program will have hundreds of variables and you have to create and memorize variable names, this will be one hell of a problem. 

For this, there is the 'this', which references - ie points - the class itself elements! 
It's pretty easy to use, our constructor woul be like: 

 public Employee (String name, int id, double salary) { 
 this.name = name; 
 this.ID = id; 
 this.salary = salary; 
 } 

Ready. 
Now it was obvious that 'this.nome' is the variable 'name' of class 'Employee' and 'name' is the variable that the was given to that class in order to create an object! 

We use the 'this' inside the class. So whenever we put 'this.' before a variable is implied to Java that we are referring to the attributes of that class. 
We can use even in a print, if it is within the class. In one method, for example, as discussed below. 

Another use of 'this' is to pass the current object as a parameter. 
public Object getObjeto() { 
return this; 
} 

Another example is create the method ...: 
public String toString() 

 ...in its class, and use 'this' inside a print. The Java will understand that what aimed to be printed is the String within this method 'toString ()'. 

Another use of 'this' is to invoke other constructors. To invoke a constructor within another, this call must be the first statement of the current constructor. 
For example, by simply: 
this; 

We are calling the default constructor that takes no parameters. 
Making: 
this (2112); 

We are invoking the constructor that takes an integer as a parameter. 


Invoking methods of created objects

Let's create a method within the class "Employee", which displays all information of an object of that class.Let's call 'display': 

public void display() { 
 System.out.printf ("The employee %s, ID number %d, receives %.2f  ach month", this.nome, this.ID, this.salary); 
} 


To use, simply put '.display()' after the name of the object, which will make this method run. 
Note, however, that as we explained in our article on Classes and Objects , this class is only an abstraction.
That is, this method does not really exist! It will only come into existence when we create an object of that class! 
(Actually it can exist if the class was static. Will study this soon). 

To illustrate the method invocation of an object and use the 'this', we will create an official - an object - called 'boss'. 
The code looks like this: 

thisMethod.java

public class thisMethod{
    
    public static void main(String[] args){
        String name = "Neil Peart";
        int ID=2112; 
        double salary = 1000000;
        
        Funcionario boss = new Funcionario(name, ID, salary);
        
        chefe.display();
        }

}



Employee.java
public class Employee {
    private String name;
    private int ID;
    private double salary;
    
    public Funcionario( String name, int ID, double salary){
        this();
 System.out.println(this);

        this.name = name;
        this.ID = ID;
        this.salary = salary;
    }
    
    public String toString(){
 return "Command ran: System.out.println(this)";
    }

    public void display(){
        System.out.printf("The employee %s, with ID %d earn U$%.2f per month", this.name,this.ID,this.salary);

}

No comments: