diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php
index 61982c3885..e2431b2723 100644
--- a/core/lib/Drupal.php
+++ b/core/lib/Drupal.php
@@ -320,10 +320,20 @@ public static function cache($bin = 'default') {
    * One common usecase is to provide a class which contains the actual code
    * of a hook implementation, without having to create a service.
    *
-   * @return \Drupal\Core\DependencyInjection\ClassResolverInterface
-   *   The class resolver.
+   * @param string $class
+   *   (optional) A class name to instantiate.
+   *
+   * @return \Drupal\Core\DependencyInjection\ClassResolverInterface|object
+   *   The class resolver of if $class is provided, a class instance with a
+   *   given class definition.
+   *
+   * @throws \InvalidArgumentException
+   *   If $class does not exist.
    */
-  public static function classResolver() {
+  public static function classResolver($class = NULL) {
+    if ($class) {
+      return static::getContainer()->get('class_resolver')->getInstanceFromDefinition($class);
+    }
     return static::getContainer()->get('class_resolver');
   }
 
diff --git a/core/modules/field_layout/field_layout.module b/core/modules/field_layout/field_layout.module
index 5a5fa11a49..9025cb307a 100644
--- a/core/modules/field_layout/field_layout.module
+++ b/core/modules/field_layout/field_layout.module
@@ -50,8 +50,7 @@ function field_layout_entity_type_alter(array &$entity_types) {
  */
 function field_layout_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
   if ($display instanceof EntityDisplayWithLayoutInterface) {
-    \Drupal::classResolver()->getInstanceFromDefinition(FieldLayoutBuilder::class)
-      ->buildView($build, $display);
+    \Drupal::classResolver(FieldLayoutBuilder::class)->->buildView($build, $display);
   }
 }
 
@@ -62,8 +61,7 @@ function field_layout_form_alter(&$form, FormStateInterface $form_state, $form_i
   $form_object = $form_state->getFormObject();
   if ($form_object instanceof ContentEntityFormInterface && $display = $form_object->getFormDisplay($form_state)) {
     if ($display instanceof EntityDisplayWithLayoutInterface) {
-      \Drupal::classResolver()->getInstanceFromDefinition(FieldLayoutBuilder::class)
-        ->buildForm($form, $display);
+      \Drupal::classResolver(FieldLayoutBuilder::class)->buildForm($form, $display);
     }
   }
 }
diff --git a/core/modules/inline_form_errors/inline_form_errors.module b/core/modules/inline_form_errors/inline_form_errors.module
index dcb140b7a5..3bacd37b93 100644
--- a/core/modules/inline_form_errors/inline_form_errors.module
+++ b/core/modules/inline_form_errors/inline_form_errors.module
@@ -59,8 +59,7 @@ function inline_form_errors_preprocess_datetime_wrapper(&$variables) {
  * Implements hook_element_info_alter().
  */
 function inline_form_errors_element_info_alter(array &$info) {
-  \Drupal::classResolver()->getInstanceFromDefinition(RenderElementHelper::class)
-    ->alterElementInfo($info);
+  \Drupal::classResolver(RenderElementHelper::class)->alterElementInfo($info);
 }
 
 /**
diff --git a/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install b/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
index 99ea0d1be5..8b94cc82f5 100644
--- a/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
+++ b/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
@@ -12,7 +12,7 @@
  */
 function demo_umami_content_install() {
   if (!\Drupal::service('config.installer')->isSyncing()) {
-    \Drupal::classResolver()->getInstanceFromDefinition(InstallHelper::class)->importContent();
+    \Drupal::classResolver(InstallHelper::class)->importContent();
   }
 }
 
@@ -21,6 +21,6 @@ function demo_umami_content_install() {
  */
 function demo_umami_content_uninstall() {
   if (!\Drupal::service('config.installer')->isSyncing()) {
-    \Drupal::classResolver()->getInstanceFromDefinition(InstallHelper::class)->deleteImportedContent();
+    \Drupal::classResolver(InstallHelper::class)->deleteImportedContent();
   }
 }
diff --git a/core/tests/Drupal/Tests/Core/DrupalTest.php b/core/tests/Drupal/Tests/Core/DrupalTest.php
index 2ff6ebf666..53f40af8f3 100644
--- a/core/tests/Drupal/Tests/Core/DrupalTest.php
+++ b/core/tests/Drupal/Tests/Core/DrupalTest.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\Tests\Core;
 
+use Drupal\Core\DependencyInjection\ClassResolverInterface;
 use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
 use Drupal\Core\Entity\EntityStorageInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -120,8 +121,21 @@ public function testCache() {
    * @covers ::classResolver
    */
   public function testClassResolver() {
-    $this->setMockContainerService('class_resolver');
-    $this->assertNotNull(\Drupal::classResolver());
+    $class_resolver = $this->prophesize(ClassResolverInterface::class);
+    $this->setMockContainerService('class_resolver', $class_resolver->reveal());
+    $this->assertInstanceOf(ClassResolverInterface::class, \Drupal::classResolver());
+  }
+
+  /**
+   * Tests the classResolver method when called with a class.
+   *
+   * @covers ::classResolver
+   */
+  public function testClassResolverWithClass() {
+    $class_resolver = $this->prophesize(ClassResolverInterface::class);
+    $class_resolver->getInstanceFromDefinition(static::class)->willReturn($this);
+    $this->setMockContainerService('class_resolver', $class_resolver->reveal());
+    $this->assertSame($this, \Drupal::classResolver(static::class));
   }
 
   /**
