Java Programming/Keywords/this

From Wikibooks, open books for an open world
< Java Programming | Keywords
Jump to: navigation, search

this is a Java keyword. It contains the current object reference.

Syntax:

this.method();
or
this.variable;

Example #1:

Computer code
public class MyClass
 {
    //...
    private String _memberVar;
    //...
    public void setMemberVar( String value )
    {
        this._memberVar= value;
    }
 }

Example #2:

Computer code
public class MyClass
 {
    MyClass(int a, int b) {
        System.out.println("int a: " + a);
        System.out.println("int b: " + b);
    }
    MyClass(int a) {
        this(a, 0);
    }
    //...
    public static void main(String[] args) {
        new MyClass(1, 2);
        new MyClass(5);
    }
 }
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export