Create a Java class or type

With the Create New Class dialog and file templates, AndroidStudio helps you to quickly create the following new classes and types:

  • Java classes
  • Enumeration and singleton classes
  • Interface and annotation types
    After you fill in the Create New Class dialog fields and clickOK, Android Studio creates a .java file containingskeleton code, including a package statement, any necessary imports, a header,and a class or type declaration. Next, you can add your code to this file.

File templates specify how Android Studio generates the skeleton code. You canuse the file templates provided with Android Studio as is, or customize them tosuit your development process.

The Android Studio dialog and templates now differ from the IntelliJ IDEA versions.If you customized file templates in Android Studio prior to version 2.2, youneed to modify your templates to comply with Android Studio requirements if youwant to use the additional fields in the dialog.

Viewing and customizing file templates

Android Studio provides file templates that determine how new Java classes andtypes are created with the Create New Class dialog. You cancustomize these templates.

The dialog provided in Android Studio 2.1.x or lower hadName and Kind fields.The dialog provided in Android Studio 2.2 and higher has more options, includingSuperclass, Interface(s),Package, Visibility,Modifiers, and Show Select Overrides Dialog,as shown in figure 1.

Create a Java class or type - 图1

Figure 1. The Create New Classdialog in Android Studio 2.2 or higher.

The Android Studio file templates include Velocity Template Language (VTL) codeand variables that handle these additional options.The Create New Class dialog uses the AnnotationType,Class,Enum, Interface, and Singletonfile templates.

If you haven't customized a file template used by the Create New Class dialog,Android Studio automatically updates the template when you install version2.2 or higher.

If, prior to Android Studio 2.2, you did customize a file template used bythe Create New Class dialog, you need to modify the template if you wantto use the new dialog options, as described next.

To view the templates, find customizations, and modify the templates, followthese steps:

  • In the Project window, select a Java file or folder, andthen select File > New > Edit FileTemplates.
    Alternatively, for Windows or Linux, select File >Settings > Editor > File and CodeTemplates > Files. For Mac, select AndroidStudio > Preferences > Editor >File and Code Templates > Files.

The Fileand Code Templates dialog, or the Preferences orSettings dialog, appears. In the template list, internaltemplate names are in bold font. Customized template names are displayed in ahighlight color, such as blue.

  • Customize the file templates as needed.
    Make sure your changes comply with theAndroid Studio file template code if you want to use theCreate New Class dialog fields.If you’ve customized any filetemplates prior to Android Studio 2.2, andthey’re used by the Create New Class dialog, you need to updateyour templates if you want to use the additional dialog options. Compare your template with theAndroid Studio file template code, and thenintegrate the portions of the Android Studio template that apply to the dialog.

For more information about file templates, including VTL, see Fileand Code Templates and Fileand Code Templates Dialog.

Creating a Java class or type

Android Studio helps you to create new Java classes; enumeration and singletonclasses; and interface and annotation types based on file templates.

To create a new Java class or type, follow these steps:

  • In the Project window, right-click a Java file or folder,and select New > Java Class.
    Alternatively, select a Java file or folder in the Projectwindow, or click in a Java file in the Code Editor. Then selectFile > New > Java Class.

The item you select determines the default package for the new class or type.

  • In the Create New Class dialog, fill in the fields:
  • Name - The name of the new class or type. It must complywith Java name requirements. Don’t type a file name extension.
  • Kind - Select the category of class or type.
  • Superclass - The class that your new class inherits from.You can type the package and class name, or just the class name and thendouble-click an item in the drop-down list to autocomplete it.
  • Interface(s) - One or more interfaces that the new class ortype implements. Multiple interfaces should be separated by a comma followed by anoptional space. You can type the package and interface name, or just theinterface name and then double-click an item in the drop-down list toautocomplete it.
    Autocomplete works for the first interface name only. Note that whilethe comma and the following interface name can bring up a tooltip error, you can ignore the error because it doesn’t affect the generated code.

  • Package - The package that the class or type will residein. The default automatically appears in the field. If you type a package namein the field, any portions of the package identifier that don’t exist arehighlighted red; in this case, Android Studio creates the package after youclick OK. This field must contain a value; otherwise, the Javafile won’t contain a package statement, and the class or type won’t be placed within a package in the project.
    The default depends on how youlaunched the Create New Class dialog. If you first selected aJava file or folder in the Project window, the default is thepackage for the item you selected. If you first clicked in a Java file in the Code Editor, the default is the package that contains this file.

  • Visibility - Select whether the class or type is visible toall classes, or just to those in its own package.

  • Modifiers - Select the Abstract orFinal modifier for a Class, or neither.
  • Show Select Overrides Dialog - For a Kindof Class, check this option to open the SelectMethods to Override/Implement dialog after you click OK. Inthis dialog, you can select methods that you would like to override orimplement, and Android Studio will generate skeleton code for these methods.
    Any fields that don’t apply to the Kind are hidden.

  • Click OK.
    Android Studio creates a Java file with skeleton code that you can modify. Itopens the file in the Code Editor.

Note: You can create a singleton class by selectingFile > New > Singleton orFile > New > Java Class; thelatter technique offers more options.

Android Studio file templates

This section lists the Android Studio file template code written in the VTL scripting language, followedby definitions of the variables. The values that you provide in theCreate New Class dialog become the variable values in the template.Note that the lines that begin with#if (${VISIBILITY} extend all the way to the open brace ({ ).

AnnotationType file template

  1. #if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
  2.  
  3. #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
  4. #end
  5. #parse("File Header.java")
  6.  
  7. #if (${VISIBILITY} == "PUBLIC")public #end @interface ${NAME} #if (${INTERFACES} != "")extends ${INTERFACES} #end {
  8. }

Class file template

  1. #if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
  2.  
  3. #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
  4. #end
  5. #parse("File Header.java")
  6.  
  7. #if (${VISIBILITY} == "PUBLIC")public #end #if (${ABSTRACT} == "TRUE")abstract #end #if (${FINAL} == "TRUE")final #end class ${NAME} #if (${SUPERCLASS} != "")extends ${SUPERCLASS} #end #if (${INTERFACES} != "")implements ${INTERFACES} #end {
  8. }

Enum file template

  1. #if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
  2.  
  3. #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
  4. #end
  5. #parse("File Header.java")
  6.  
  7. #if (${VISIBILITY} == "PUBLIC")public #end enum ${NAME} #if (${INTERFACES} != "")implements ${INTERFACES} #end {
  8. }

Interface file template

  1. #if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
  2.  
  3. #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
  4. #end
  5. #parse("File Header.java")
  6.  
  7. #if (${VISIBILITY} == "PUBLIC")public #end enum ${NAME} #if (${INTERFACES} != "")implements ${INTERFACES} #end {
  8. #end {
  9. }

Singleton file template

  1. #if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
  2.  
  3. #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
  4. #end
  5. #parse("File Header.java")
  6.  
  7. #if (${VISIBILITY} == "PUBLIC")public #end class ${NAME} #if (${SUPERCLASS} != "")extends ${SUPERCLASS} #end #if (${INTERFACES} != "")implements ${INTERFACES} #end {
  8. private static final ${NAME} ourInstance = new ${NAME}();
  9.  
  10. #if (${VISIBILITY} == "PUBLIC")public #end static ${NAME} getInstance() {
  11. return ourInstance;
  12. }
  13.  
  14. private ${NAME}() {
  15. }
  16. }

File template variables

Android Studio replaces file template variables with values in the generatedJava file. You enter the values in the Create New Class dialog.The template has the following variables that you can use:

  • IMPORT_BLOCK - A newline-delimited list of Javaimport statements necessary to support any superclass orinterfaces, or an empty string (""). For example, If you onlyimplement the Runnable interface and extend nothing, this variablewill be "import java.lang.Runnable;\n". If you implement theRunnable interface and extend the Activity class, it will be"import android.app.Activity;\nimportjava.lang.Runnable;\n".
  • VISIBILITY - Whether the class will have public access or not.It can have a value of PUBLIC or PACKAGE_PRIVATE.
  • SUPERCLASS - A single class name, or empty. If present, therewill be an extends ${SUPERCLASS} clause after the new class name.
  • INTERFACES - A comma-separated list of interfaces, or empty. Ifpresent, there will be an implements ${INTERFACES} clause after thesuperclass, or after the class name if there’s no superclass. For interfaces andannotation types, the interfaces have the extends keyword.
  • ABSTRACT - Whether the class should be abstract or not. It canhave a value of TRUE or FALSE.
  • FINAL - Whether the class should be final or not. It can have avalue of TRUE or FALSE.