How do you use AutoCloseable with a simple code example?

This example demonstrates how to use the AutoCloseable interface in Java to ensure that resources are closed automatically.
Java, AutoCloseable, Resource Management, Try-With-Resources

        class MyResource implements AutoCloseable {
            public void doSomething() {
                System.out.println("Resource is being used.");
            }
            @Override
            public void close() {
                System.out.println("Resource is closed.");
            }
        }

        public class Main {
            public static void main(String[] args) {
                try (MyResource resource = new MyResource()) {
                    resource.doSomething();
                } // Automatically calls resource.close() here
            }
        }
    

Java AutoCloseable Resource Management Try-With-Resources