0

I am really confused about the keyword "this" and without "this" on classes, because i think it is the same :

public class thisthing {
    public int a;
    public arrays(){
        a=1;

    }
    public void A(){
        this.a=10;
    }
    public void printa(){
        System.out.println(a);
        System.out.println(this.a);
    }
}

if I call printa without calling A() I get this results :

1
1

but if i do the same with calling A() :

10
10

What is the difference between these two?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Zara suto
  • 1
  • 1
  • 3

5 Answers5

4

Here there is no difference. For a difference, you would need a local variable with that name. For example,

public void printa(int a){
    System.out.println(a); // the argument a
    System.out.println(this.a); // the field a
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

this refers to the current object - the instance of the class in whose method is the scope.

In the scope of the method you could see local variables, member variables and static variables. If you have a name collision between a local variable and member variable you could use this to differentiate between the two - you explicitly state that you work with the member variable.

In case you don't have a local variable with matching name you could not use the this reference - the member variable would be recognised easily.

Example:

new MyClass().printAVar(1);

class MyClass {
    private int x = 2;

    public void printAVar(int x) {
        // this.x == 2;
        // x == 1;
    }
}
stan0
  • 11,549
  • 6
  • 42
  • 59
1

What is the difference between these two?

If an identifier unambiguously refers to an instance variable in the class, there is no difference between using this and not. However, if the instance variable is shadowed by a local variable or a function argument, using this forces the compiler to use the instance variable instead. For example

public class Main 
{
    int a = 1;

    void bar(int a)
    {
        System.out.println("local " + a);
        System.out.println("instance " + this.a);
    }

    public static void main(String[] args)
    {
        new Main().bar(2);
    }
} 

Prints

local 2
instance 1

I find this is most useful when assigning values in a constructor. You don't have to think of new variable names for the constructor arguments.

public class Main {

    int a;

    Main(int a)
    {
        this.a = a; // Assigns the instance variable from the argument
    }

    void bar(int a)
    {
        System.out.println("local " + a);
        System.out.println("instance " + this.a);
    }

    public static void main(String[] args)
    {
        new Main(3).bar(2);
    }
}

Prints

local 2
instance 3
JeremyP
  • 84,577
  • 15
  • 123
  • 161
1

"this" is a keyword. "this" is a reference variable that refers to the current object.

without "this"

class Student{
String name;  
void data(String name){  
name=name; 
} 
void disp(){   
System.out.println("Name:"+name); 
} 
public static void main(String args[]){  
Student s1=new Student();
s1.data("A");
s1.disp();  
}}

Output

Output:- Name:null

Now using "this" keyword

class Student {
String name;  
void data(String name){  
this.name=name; 
} 
void disp(){   
System.out.println("Name:"+name); 
} 
public static void main(String args[]){  
Student s1=new Student();
s1.data("A");
s1.disp();  
}}

Now Output will be

Output:- Name:A
Community
  • 1
  • 1
rk13
  • 79
  • 1
  • 5
0

this is used to say you want the "a" from the class, not the parameter

 public void printa(int a){
        System.out.println(a); //print the parameter of function
        System.out.println(this.a); //print the attribute of the class
    }

for example, if you call printa(2) :

2
1
Kepotx
  • 1,095
  • 12
  • 26