1
votes

I want to add a sentence to all new classes that I create in java. By default Java entry point comes like this:

public static void main(String[] args) {

}

and I want to achive something like this:

public static void main(String[] args) {
Scanner key = new Scanner(System.in); 
}

Is this possible? Thank you!

Eclipse Java EE IDE for Web Developers. Version: Mars Release (4.5.0) Build id: 20150621-1200

2
Sure. How you do it depends on your IDE, though, which you haven't mentioned here.azurefrog
Java does not create classes. You are free to create new files however you like.SLaks
This is the entry point, not the constructor. You can change the body of this method, but not the name and parameters.Andrew Williamson
@SLaks Well, IDEs like eclipse have a wizard that will generate a class based on a template you configure. You could set that to have whatever base code and comments you like.azurefrog
main() is not a constructor, it's a method that will be executed when running your programme.Youssef NAIT

2 Answers

0
votes

In Eclipse it is possible to edit built-in code templates (the one used by Eclipse e.g. for new class files) and create your own named snippets (for fast access to often-reused templates).

Code Templates

Go to Window > Preferences > Java > Code Style > Code Templates, then to Code > New Java files. Adapt this or some other template to suit your needs.

Templates

Usually it's better though to add a smaller template via Window > Preferences > Java > Editor > Templates and use it wherever you need. Smaller bricks are more reusable then hardcoded class templates. Those are applied with <type-beginning-of-template-name + ctrl + space.

enter image description here