literally New To Java?
Rayan: Hey, I just learned Java, and it is very cool! do you Want me to show you how to write your first Java program?
Amine: Yeah, Sure, what’s the program about?
Rayan: I will keep it simple and I will write a program that says “Hello World” on the screen.
Amine: It sounds good, let us start!
Rayan: First, I have to say that Java is all about objects. This means everything in Java is treated like an object. Let me show you the code. The first step is to create a file with the “.java” extension. In this file, we’ll make a class. It’s best to name the class the same as the file name
class helloworld{
}Amine: Alright, we have a file named “helloworld.java” with a class named “helloworld” inside. What’s next?
Rayan: Okay, next, we’ll try to compile this file and see what happens.
javac helloworld.javaAmine: What does this command do?
Rayan: This command compiles the Java file. It reads our code and translates it into a language that the computer can understand. When we run this command, we get a new file named “helloworld.class,” which is the compiled version of our code. It runs on the Java Virtual Machine (JVM), but I’ll explain more about that later.
Amine: Alright, what do we do after compiling the code? How do we run it?
Rayan: To run the code, we use the following command:
java helloworldAmine: Let’s see what we get on the screen.
Error: Main method not found in class helloworld, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Applicationok we got this because we did not define the main method in our code the main method is the entry point of our program.
class helloworld{
public static void main(String[] args){
System.out.println("Hello World");
}
}Amine: Okay, let’s compile the code and run it.
Get Anas Bouzanbil’s stories in your inbox
Join Medium for free to get updates from this writer.
Rayan: Well, we got the output “Hello World” on the screen.
Amine: Hmm, what if I want to add another function to the code?
Rayan: You can add as many functions as you want, but you have to call them from the main method.
Amine: Okay, let’s add another function to the code.
class helloworld{
public static void main(String[] args){
System.out.println("Hello World");
print();
}
public static void print(){
System.out.println("This is a new function");
}
}Rayan: And we got this output:
Hello World
This is a new functionAmine: Okay, I got it now. But what do the “public” and “static” keywords mean?
Rayan: The “public” keyword is an access modifier that allows the main method to be called from outside the class, and the “static” keyword allows the main method to be called without creating an object of the class.
Amine: Hmm, well, what if I want to add another class to the code and call it from the main method?
Rayan: You can add as many classes as you want, but you have to call them from the main method.
Amine: Okay, can you do it?
Rayan: Sure, let’s add another class to the code.
class helloworld{
public static void main(String[] args){
System.out.println("Hello World");
print();
newclass obj = new newclass();
obj.print();
}
public static void print(){
System.out.println("This is a new function");
}
}
class newclass{
public void print(){
System.out.println("This is a new class");
}
}Amine: Okay, I get the idea now. I have a basic idea of how to write a Java program and start working with it. Thank you, Rayan.
Rayan: You’re welcome, Amine. I’m happy to help you.









