Take the 2-minute tour ×

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!

share|improve this question

2 Answers 2

up vote 18 down vote accepted

If you have a class A.

class A{
	A(int a |){}

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


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;

share|improve this answer
Cheers, this is exactly what I was looking for. –  Benj Nov 24 '09 at 10:23
Man, I love Eclipse. Thanks! +1 –  Matt Ball Feb 9 '12 at 20:03
How to use this with multiple fields? For instance if I have 5 fields and I want to generate this fields only in one move –  pleerock Oct 30 '12 at 12:38

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

share|improve this answer
Very useful, I'm sure I'll use this even though it wasn't quite what I was after today. –  Benj Nov 24 '09 at 10:24

Your Answer


