Create a Java Class or Type _ Android Studio _ Android Developers
Create a Java Class or Type _ Android Studio _ Android Developers
com/studio/write/create-java-class
• Java classes
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.
1 of 7 24/06/25, 5:29 pm
Create a Java class or type | Android Studio | Android Developers https://developer.android.com/studio/write/create-java-class
The Android Studio file templates include Velocity Template Language (VTL
(https://velocity.apache.org/engine/)) code and variables that handle these additional options.
The Create New Class dialog uses the AnnotationType, Class, Enum, Interface, and
Singleton file templates.
To view the templates, find customizations, and modify the templates, follow these steps:
• For Windows or Linux, select File > Settings > Editor > File and Code Templates
> Files.
• For macOS, select Android Studio > Preferences > Editor > File and Code
Templates > Files.
If you want to use the Create New Class dialog fields, make sure your changes comply
with the Android Studio file template code (#templates).
For more information about file templates, including VTL, see File and Code Templates
(https://www.jetbrains.com/help/idea/2024.2/file-and-code-templates.html) and File and Code
Templates Dialog (https://www.jetbrains.com/help/idea/2024.2/settings-file-and-code-templates.html).
2 of 7 24/06/25, 5:29 pm
Create a Java class or type | Android Studio | Android Developers https://developer.android.com/studio/write/create-java-class
1. 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 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.
• Name - The name of the new class or type. It must comply with Java name
requirements. Don’t type a file name extension.
• 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
3 of 7 24/06/25, 5:29 pm
Create a Java class or type | Android Studio | Android Developers https://developer.android.com/studio/write/create-java-class
• Show Select Overrides Dialog - For a Kind of Class, check this option to open
the Select Methods to Override/Implement dialog
(https://www.jetbrains.com/help/idea/2024.2/overriding-methods-of-a-superclass.html)
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.
3. Click OK.
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.
4 of 7 24/06/25, 5:29 pm
Create a Java class or type | Android Studio | Android Developers https://developer.android.com/studio/write/create-java-class
#parse("File Header.java")
5 of 7 24/06/25, 5:29 pm
Create a Java class or type | Android Studio | Android Developers https://developer.android.com/studio/write/create-java-class
#parse("File Header.java")
private ${NAME}() {
}
}
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:
6 of 7 24/06/25, 5:29 pm
Create a Java class or type | Android Studio | Android Developers https://developer.android.com/studio/write/create-java-class
• VISIBILITY - Whether the class will have public access or not. It can have a value of
PUBLIC or PACKAGE_PRIVATE .
• ABSTRACT - Whether the class should be abstract or not. It can have a value of TRUE or
FALSE .
• FINAL - Whether the class should be final or not. It can have a value of TRUE or FALSE .
Content and code samples on this page are subject to the licenses described in the Content License (/license).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
7 of 7 24/06/25, 5:29 pm