Search code examples
javaintellij-ideacode-generationintellij-14

Is there any way to add "this" key word automatically to class fields in Intellij Idea?


For example, instead of this:

public String getName(){
    return name;
}

I want to get this:

public String getName(){
    return this.name;
}

It appears I used such setting in Eclipse, but I can't find it for Idea...


Solution

  • Yes, you can use an inspection for that.

    • search inspections for "this"
    • enable "Instance field access not qualified with 'this'" in "Code style issues" (see capture below)

    enable "Instance field access not qualified with 'this'"

    Now IntelliJ Idea will highlight not qualified access and you can refactor (ALT-ENTER under linux) to fix the issue (see capture below)

    fix the issue

    (screenshots from IntelliJ Idea 12 under linux)