Certainly! Let’s create a simple Java class that includes a main method. This class will be named MyClass. The main method is the entry point for the Java application.
public class MyClass {
public static void main(String[] args) {
// Your code goes here
System.out.println("Hello, World!"); // Example: Print "Hello, World!"
}
}
In this example:
- The class is named
MyClass. - The
mainmethod is declared aspublic static void main(String[] args). - Inside the
mainmethod, there is a line of code (System.out.println("Hello, World!");) that prints “Hello, World!” to the console. You can replace this line with your own code.
Save this code in a file named MyClass.java. To run the program, open a command prompt or terminal, navigate to the directory where the file is located, and use the following commands:
javac MyClass.java // Compile the Java source file
java MyClass // Run the compiled Java program
This will compile and execute the Java program, and you should see the output “Hello, World!” on the console. Feel free to modify the code inside the main method to suit your needs.