Test Your Knowledge
Take this quiz to reinforce your understanding of creating a Java program in Eclipse.
Introduction:
Welcome to Session 2! In this session, we will guide you through creating your very first Java program using Eclipse. We’ll also break down the basic structure of a Java program so you can understand how everything fits together.
Step 1: Setting Up Eclipse
Before we dive into the code, ensure you have Eclipse IDE installed on your computer. If not, download and install it from here.
Step 2: Create a New Java Project
- Open Eclipse.
- Go to
File
>New
>Java Project
. - Name your project:
FirstJavaProgram
. - Click Finish. You now have a new Java project set up!
Step 3: Create a Java Class
- Right-click on the
src
folder in your project. - Go to
New
>Class
. - Name your class:
HelloWorld
. - Check the box for
public static void main(String[] args)
. - Click Finish.
You’ve now created a Java class with the main method, where your code will be executed.
Step 4: Write Your First Java Program
Replace the default code with the following:
public class HelloWorld { public static void main(String[] args) { // This line prints "Hello, World!" to the console System.out.println("Hello, World!"); } } (code-box)
Step 5: Run the Program
- Click the
Run
button (the green play button) at the top of the Eclipse window. - Check the Console: You should see the text
Hello, World!
printed.
Congratulations! You’ve just run your first Java program in Eclipse.
Explaining the Program Structure
Let’s break down the code:
public class HelloWorld
: This declares a class namedHelloWorld
. Every Java program must have at least one class.public static void main(String[] args)
: This is the main method where your program starts running. Without this, your program won’t execute.System.out.println("Hello, World!");
: This line prints the text"Hello, World!"
to the console. TheSystem.out.println
method is used to display output.
Key Points to Remember
- Java programs start executing from the
main
method. - Classes and methods must be enclosed in curly braces
{ }
. - Every statement in Java ends with a semicolon
;
.
Conclusion
Now you know how to create, run, and understand the basic structure of a Java program using Eclipse. Keep experimenting with different print statements and see what you can create!
Call to Action
Try modifying the System.out.println
line to print your name or a different message. What happens when you remove the semicolon at the end of a statement? Experiment and learn!