Java Fundamentals Prework Writing Your First Java App
Hello, friend
15 minFor this exercise, you’ll build a classic app to get up to speed with any language - Hello, friend.
All Java files must be defined as a class, so let’s begin with a class definition. This class definition must match the file’s name, so call it Main. Write this code in your src/main/java/Main.java file now.
src/main/java/Main.java
public class Main {
}
By convention, class names will be written in upper camel case (also referred to as Pascal case) - each word is joined so there are no spaces or characters between them, and every word starts with an uppercase letter.
Then, all Java programs require a main() method representing the program’s entry point. This method will automatically be invoked when you run your Java app. The following method must be placed inside the Main class definition:
src/main/java/Main.java
public static void main(String[] args) {
}
So what’s going on here? Let’s break down the syntax:
![public static void main(String[] args) public is labeled with a 1, static is labeled with a 2, void is labeled with a 3, main() is labeled with a 4, and String[] args is labeled with a 5.](/modular-curriculum-all-courses/java-fundamentals-prework/writing-your-first-java-app/assets/main-syntax.png)
1. public
First, the public keyword, used for the Main class and the main() method, declares these to be available anywhere. On the other hand, a private method or class would only be available to other members of the same class. You can find more details here.
2. static
Next, the static keyword indicates that this method belongs to the Main class. The opposite of a static method would be an instance method, where the method belongs to the objects the class creates. To run an instance method, you must create a new class instance with the new keyword and call the method on the instantiated object.
3. void
void indicates the data type of the method’s return value, which will be nothing at all in the case of this method. Methods in Java require you to describe the data type of the return value of a method in their definition to enable future type-checking.
4. main()
Finally, you can name your method. In this case, the main() method is absolutely required in Java programs and must be named main. By convention, methods will be named in lower camel case - each word is joined so there are no spaces or characters between them, and every word after the first word starts with a capital letter.
5. String[] args
Inside the parentheses, you’ll notice that the method takes in one parameter: String[] args. String indicates the type, and the array brackets tell the method to accept a list of Strings. The parameter is named args by convention. This array represents any command line arguments you pass when running the method - even though the app you’re writing won’t do anything with these arguments they must be included.
This might be a lot to take in initially, but this syntax will become more manageable with practice. Let’s write the one line of code that will do what you want: Print Hello, friend to the console.
Print Hello, Friend to the console
5 min
How do you do that? Take a moment to figure out how to print to the console by doing some independent research. You may want to use Google or an AI assistant.
🧠 As you complete this activity, note that the semicolon character must be used at the end of each statement in Java.
Run the code
When you’re ready, run your app by clicking the green Run button above your code. The console should appear and display the text that’s printed to it!
Congrats, you just wrote your first Java app!
Comments
A quick note on comments. In Java, there are two options for writing comments:
// I'm a single-line comment. Just lil ol' me.
/*
I'm a multi-line comment.
There are several of us.
And we all get along fabulously.
*/
Comment on your code so far
5 minJump back into src/main/java/Main.java and add comments to your code explaining what the class definition means and what the main() method does in your own words, along with describing the syntax for each.