Java Notes
A Java program is written inside a class. For a basic program, the class contains a main method, and the main method contains the instructions Java runs.
A Java file is like a small folder for your program. The outer part is called a class. Inside that class, you write the instructions. For a beginner program, Java starts with the instructions inside a method named main.
Do not try to learn package, import, constructor, and every other word at once. Start by recognising the three important layers: file → class → main method.
Look at one complete program
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read this code from the outside in:
Welcome.javais the file you save on your computer.public class Welcomecreates the class—the named container for this program.mainis the place where Java begins running the program.System.out.println(...)is one instruction insidemain.
The curly brackets { } show what belongs together. The outer pair belongs to the class. The inner pair belongs to main.
The file name and class name must match
When you use public class, Java wants the file name and class name to be the same. This helps Java find the correct class quickly in a project with many files.
Look at the first line of your code. The word after class is the class name.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Here the class name is Welcome. Therefore, save this code in a file named Welcome.java.
The match has three parts:
- Same spelling:
Welcome, notWelcom. - Same capital letters:
Welcome, notwelcome. - Add
.javaonly to the file name:Welcome.java.
If you save this code as Hello.java, Java cannot continue because it expects the public Welcome class to be inside Welcome.java. The compiler message may look long, but its meaning is simply: rename the file, or rename the class so both names are the same.
| Code says | Save the file as |
|---|---|
public class Welcome | Welcome.java |
public class StudentProfile | StudentProfile.java |
public class Calculator | Calculator.java |
You can fix a mismatch in either direction:
- Keep
public class Welcomeand rename the file toWelcome.java. - Or change the code to
public class Helloand save the file asHello.java.
Capital letters matter. welcome.java and Welcome.java are different names to Java, so always copy the class name carefully before saving the file.
What is inside a class?
For a very small program, a class may contain only main. In a real program, a class often represents one thing—for example, one student. That student needs to remember information and perform actions.
Imagine a school form:
- The form stores the student's name. In Java, stored information is called a field.
- When a new student record is created, the name has to be given. The code that sets the first information is called a constructor.
- The student can introduce itself. A named action is called a method.
These names are easier to remember when you connect them to their job:
| Java word | Think of it as | Example in a Student class |
|---|---|---|
| Field | Information to remember | The student's name |
| Constructor | First setup | Give the new student a name |
| Method | An action | Print an introduction |
The following program creates one student, gives that student a name, and tells the student to introduce itself. Read the explanation below the code; do not worry about understanding every symbol on the first read.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read the program in this order:
String nameis the field. Every Student can remember a name.Student(String studentName)is the constructor. It receives the name when a new Student is created.name = studentNamesaves that received name inside this Student.introduce()is the method. It uses the saved name to print a message.- In
main,new Student("Asha")creates one Student and gives it the name Asha.learner.introduce()asks that particular Student to run its introduction method.
You do not need fields and constructors for every first program. Use them when your program needs to create something that remembers its own information, such as a student, product, bank account, or course.
What does main mean?
main is the starting point of a normal Java program. When you type a command such as java ProgramStart, Java does not run every line in the file randomly. It first looks for a method named main, then starts with the first instruction inside that method.
Think of main as the Start button for your program. The code inside its curly brackets runs from top to bottom.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Run the program. Java enters main, prints the first line, then the second, then the third. If you put another println line inside main, Java runs that too. If you write a line outside the class or outside a method, Java reports an error because it does not know when to run it.
Why is the full line so long?
public static void main(String[] args)At this stage, treat the full line as Java's required start label. Copy it exactly whenever you create a normal runnable Java program. Each word has a job, but you do not need all those jobs yet:
| Part | Simple idea for now |
|---|---|
main | The name Java looks for when the program starts. |
String[] args | A place where values typed after the program name can arrive. |
| Everything else | Required words that make this standard start method work. |
The important rule today is: write main in lower-case letters and keep the rest of the line exactly as shown. You will learn public, static, and void after classes and methods make more sense.
Package and import: use them later
Small programs in this course work without a package or import. When a project grows to many files, Java needs a way to keep files organised and to use useful code that already exists in Java. That is where package and import help.
Package means a labelled group of related classes
Think of a package as a labelled folder in a cupboard. A school application might keep Student-related classes together and Payment-related classes together. The package line is written at the very top of a Java file.
package school.app;school.app is only a name chosen by the programmer. It says, “this class belongs to the school app group.” In a real project, the file is also placed inside matching folders: school/app.
You do not need to add packages to the small one-file programs you are writing now. Use them later when a project has several related classes.
Import means “I want to use this ready-made Java class”
Java already provides many helpful classes. For example, LocalDate can give today's date. An import line lets you use the short name LocalDate instead of writing its full Java address every time.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read it like this:
import java.time.LocalDate;tells Java that this program wants to use the ready-madeLocalDateclass.LocalDate.now()asks that class for today's date.- The program prints that date.
Without the import, you would have to write the longer name java.time.LocalDate every time. String and System do not need imports because Java makes them available automatically.
The beginner rule
For now, add an import only when an example tells you to. Do not write a package line in the basic live-editor programs unless the lesson specifically uses one. First become comfortable with classes and main; packages and imports will feel natural once you work with more than one class.
Check yourself
Take the Welcome program and do these three changes:
- Rename the class to
MyFirstProgram. - Save the file as
MyFirstProgram.javaif using your computer. - Change the printed message and run it.
If it runs, you understand the basic structure: file name, class name, main, and an instruction inside main.
Continue the lesson path