tags:

views:

76

answers:

2

When I'm coding in eclipse, I like to be as lazy as possible. So I frequently type something like:

myObject = new MyClass(myParam1, myParam2, myParam3);

Even though MyClass doesn't exist and neither does it's constructor. A few clicks later and eclipse has created MyClass with a constructor inferred from what I typed. My question is, is it possible to also get eclipse to generate fields in the class which correspond to what I passed to the constructor? I realize it's super lazy, but that's the whole joy of eclipse!

+1  A: 

I know you can do the other way round. Define the fields and let Eclipse generate a constructor using these fields for you: Source | Generate Constructor using Fields

Grzegorz Oledzki
Very useful, I'm sure I'll use this even though it wasn't quite what I was after today.
Benj
+4  A: 

If you have a class A.

class A{
 A(int a |){}
}

| is the cursor. Crtl + 1 "assign parameter to new field"

Result:

class A{
 private final int a;
 A(int a){
  this.a = a;
 }
}

This works also for methods:

 void method(int b){}

Will result in:

 private int b;
 void method(int b){
  this.b = b;

 }
Thomas Jung
Cheers, this is exactly what I was looking for.
Benj