Abe
Abe

Reputation: 9031

Eclipse shortcut to automatically declare the class and variable from method return type

Does anybody know of a short cut in Eclipse that will put in the class and variable based on a method return type?

For e.g.

SomeClass{
  public AnotherClass getAnotherClass(){
  }
}

MainClass{
  someClassObj.getAnotherClass();// Short cut here to insert "AnotherClass anotherClass ="
}

Upvotes: 3

Views: 5318

Answers (2)

Jonas Schmid
Jonas Schmid

Reputation: 5491

This shortcut exists: CTRL + 2 and then a second key press:

  • F creates a new field
  • L creates a new variable.

Upvotes: 14

VonC
VonC

Reputation: 1327784

I don't think there is one, considering:

  • it is generally considered "bad practice" to call a method returning a value without assigning this value to a variable.
    And if you assign it, you have your AnotherClass anotherClass[=...] set in the code directly ;)

  • Even if you don't assign to a return value, you can move your mouse over the method to get its definition (including the return type).

Upvotes: 1

Related Questions