Skip to content

Most visited

Recently visited

navigation

Create a Java Class or Type

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

After you fill in the Create New Class dialog fields and click OK, Android Studio creates a .java file containing skeleton 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 can use the file templates provided with Android Studio as is, or customize them to suit 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, you need to modify your templates to comply with Android Studio requirements if you want to use the additional fields in the dialog.

Viewing and Customizing File Templates

Android Studio provides file templates that determine how new Java classes and types are created with the Create New Class dialog. You can customize these templates.

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

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

The Android Studio file templates include Velocity Template Language (VTL) code and variables that handle these additional options. The Create New Class dialog uses the AnnotationType, Class, Enum, Interface, and Singleton file 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 version 2.2 or higher.

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

To view the templates, find customizations, and modify the templates, follow these steps:

  1. In the Project window, select a Java file or folder, and then select File > New > Edit File Templates.
  2. Alternatively, for Windows or Linux, select File > Settings > Editor > File and Code Templates > Files. For Mac, select Android Studio > Preferences > Editor > File and Code Templates > Files.

    The File and Code Templates dialog, or the Preferences or Settings dialog, appears. In the template list, internal template names are in bold font. Customized template names are displayed in a highlight color, such as blue.

  3. Customize the file templates as needed.
  4. Make sure your changes comply with the Android Studio file template code if you want to use the Create New Class dialog fields. If you’ve customized any file templates prior to Android Studio 2.2, and they’re used by the Create New Class dialog, you need to update your templates if you want to use the additional dialog options. Compare your template with the Android Studio file template code, and then integrate the portions of the Android Studio template that apply to the dialog.

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

Creating a Java Class or Type

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

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

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

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

  3. In the Create New Class dialog, fill in the fields:
    • Name - The name of the new class or type. It must comply with 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 then double-click an item in the drop-down list to autocomplete it.
    • Interface(s) - One or more interfaces that the new class or type implements. Multiple interfaces should be separated by a comma followed by an optional space. You can type the package and interface name, or just the interface name and then double-click an item in the drop-down list to autocomplete it.
    • Autocomplete works for the first interface name only. Note that while the 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 reside in. The default automatically appears in the field. If you type a package name in the field, any portions of the package identifier that don’t exist are highlighted red; in this case, Android Studio creates the package after you click OK. This field must contain a value; otherwise, the Java file 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 you launched the Create New Class dialog. If you first selected a Java file or folder in the Project window, the default is the package 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 to all classes, or just to those in its own package.
    • Modifiers - Select the Abstract or Final modifier for a Class, or neither.
    • Show Select Overrides Dialog - For a Kind of Class, check this option to open the Select Methods to Override/Implement dialog after you click OK. In this dialog, you can select methods that you would like to override or implement, and Android Studio will generate skeleton code for these methods.

    Any fields that don’t apply to the Kind are hidden.

  4. Click OK.
  5. Android Studio creates a Java file with skeleton code that you can modify. It opens the file in the Code Editor.

Note: You can create a singleton class by selecting File > New > Singleton or File > New > Java Class; the latter technique offers more options.

Android Studio File Templates

This section lists the Android Studio file template code written in the VTL scripting language, followed by definitions of the variables. The values that you provide in the Create 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

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

#if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
#end
#parse("File Header.java")

#if (${VISIBILITY} == "PUBLIC")public #end @interface ${NAME} #if (${INTERFACES} != "")extends ${INTERFACES} #end {
}

Class file template

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

#if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
#end
#parse("File Header.java")

#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 {
}

Enum file template

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

#if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
#end
#parse("File Header.java")

#if (${VISIBILITY} == "PUBLIC")public #end enum ${NAME} #if (${INTERFACES} != "")implements ${INTERFACES} #end {
}

Interface file template

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

#if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
#end
#parse("File Header.java")

#if (${VISIBILITY} == "PUBLIC")public #end enum ${NAME} #if (${INTERFACES} != "")implements ${INTERFACES} #end {
#end {
}

Singleton file template

#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

#if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
#end
#parse("File Header.java")

#if (${VISIBILITY} == "PUBLIC")public #end class ${NAME} #if (${SUPERCLASS} != "")extends ${SUPERCLASS} #end #if (${INTERFACES} != "")implements ${INTERFACES} #end {
    private static final ${NAME} ourInstance = new ${NAME}();

    #if (${VISIBILITY} == "PUBLIC")public #end static ${NAME} getInstance() {
        return ourInstance;
    }

    private ${NAME}() {
    }
}

File template variables

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

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)