Adding a variable to the constructor automatically + eclipse

I am trying to find a plugin or some shortcut that exists in eclipse for this function.

Basically, is there a shortcut that, if I click, will lead to the creation of a constructor with all the public variables of the class defined in it?

So, if I have a Student class like

public class Student{ public String name; public int age; } 

Then pressing the shortcut key results in

 Student( String name , int age ){ this.age = age; this.name = name; } 

It is hard to have specific ones, but is there any function to get all assigned variables?

+6
source share
2 answers

Right click in the editor and click

Source -> Generate Constructor Using Fields

You can choose a super constructor to use, as well as select instance variables to add to the constructor.

Accepted directly from: how to create constructors in eclipse

+4
source

you can use ALT + Shift + S to create a constructor using fields (you can select the variable you want to use) or directly from the Super class

+1
source

Source: https://habr.com/ru/post/886042/


All Articles