IntelliJ IDEA has many tools for generating code. One of the first you can use is a tool to create new Java files based on templates that are included with the software.
- [Voiceover] IntelliJ IDEA has many tools for quickly generating code. One of the first important ones to learn about is a tool to create new Java files based on templates that are included with the software. In this project, named Software, I've opened the project window, and I am using the package's scope. So I'm limiting myself to only seeing my Java packages. Then I'll rick-click on my base package and select New, Java Class. Now, even though I said I wanted to create a new class, I can actually create a number of different types of files.
I can create a class, an interface, an enumeration, an annotation, a singleton, and even a Java FX application. I'll just create a class, and I'll call it MyClass. Now, the structure of this file is determined by a template, and you can modify that template. Notice that it starts with my current package, then there's an automatically generated content that contains my identity and the current date, and then there's the basic class file. To change that, go to your Settings or Preferences.
From there, go to Editor, and then File and Code Templates. Under Files you'll see a whole bunch of different templates available. This is the one I just used. At the top of the file, there's some conditional logic, that's basically saying, if there's a package and it's not empty, then declare the package at the top. Then there's a call to something called parse, that's opening a file called File Header.java. You can find that file under the Includes tab, and this is where the auto-generated comment is. Notice with this and other files, that the description down here shows a list of all the different pre-defined variables you can use.
I'm using a variable called User and another one called Date, but you can build your comment exactly the way you want it. Or, you can completely remove that from a particular kind of file. So, if I don't want that comment to appear in every new class, all I need to do is delete the parse call, and then click OK. And then, I'll once again create a new class I'll call it AnotherClass, and this one is generated without the comment. And, finally, I'll generate another kind of file, an interface. I'll call it MyInterface, and I'll set the kind to Interface and click OK.
This is using a different template, and so the comment is included now. And instead of the class keyword, it's used the the interface keyword. I'll add an abstract method here. It'll return void and I'll name it doSomething, and it'll receive a single argument that's a string named S1. Then I'll create yet another class. I'll right-click and choose New, Java Class, and I'll call this ImplementingClass, and in this class I'll implement that interface. I'll start typing "implements" right after the class name, I'll auto-complete that, then I'll press control + space, and I'll see my available interfaces.
My custom interfaces will appear before the ones from the Java run time. I'll choose MyInterface. Because I've chosen an interface with an abstract method, I see an error condition. I'll use an intention action by pressing alt + enter on Windows, or option + return on Mac, and I'll choose Implement Methods, and I see the one method is displayed there, and I'll click OK and that adds the implementation of the method with the override annotation. One of the interesting choices in IntelliJ IDEA is that when you use this feature to generate a class, there aren't a lot of details.
You simply create the class, interface, enumeration, or whatever other kind you're creating, and you give it a name. Everything else you need to do is up to you when you start coding. And that's different from some other IDE's that can generate a whole bunch of different kind of code automatically for you from this particular kind of dialogue. In IntelliJ IDEA, you create a very simple class, and then everything else is at your discretion.
- Exploring IntelliJ IDEA editions
- Installing IntelliJ IDEA on macOS and Windows
- Configuring IntelliJ IDEA
- Creating new projects
- Importing an Eclipse project
- Exploring the user interface
- Editing and debugging code
- Building, compiling, and packaging Java projects
- Managing multiple branches with Git
- Programming with Groovy, Scala, and Kotlin