1. Introduction
When you’re just starting to program, it seems like you can simply write code line by line and everything will work. But as soon as the program becomes more complex, confusion starts in your head: where things are located, why they were written, and if you need to change one line — where is it even stored?
That’s why Java, like most modern languages, offers a way to “organize” code into compartments so you can always understand what the program does and quickly find the right place.
Imagine a company. It has HR, accounting, an IT department, a logistics department, and so on. Each department has its own responsibilities, staff, and tasks.
In a Java program, each such “department” is a class. One class handles users, another — calculations, a third — working with files.
Here’s a typical structure of a simple project:
- Main.java — the head office: this is where the “entry point” lives, where everything starts.
- User.java — the department where user data is stored and processed.
- Calculator.java — the calculations department.
- FileHelper.java — the file specialists.
Even a very small program usually has at least one class. And large projects are a whole “skyscraper” of hundreds of classes!
2. A class is a toolbox
Java is an object-oriented language, and everything in it is an object. The types of objects are called classes.
A class is like a box or a cabinet where we put the things we need: variables and methods. Imagine a box labeled “Hammers”: inside are only tools related to hammers.
class Example
{
// Here will be our tools: variables and methods
}
Classes usually contain variables and functions. In Java, class variables are commonly called fields, and class functions — methods. But the meaning doesn’t change.
Remember our first code?
System.out.println("Hello, everyone!");
This code includes both a class and methods:
- System — a class that contains methods for working with the system.
- out — a field that is an object of the PrintStream class.
- println — a method of the PrintStream class that prints text to the screen.
Programming, in a sense, boils down to writing your own classes and methods that call methods of other classes.
3. Short form of the main() method
In fact, the minimal Java program looks like this:
class Program
{
public static void main(String[] args)
{
// Your code goes here
}
}
It contains at least one class, which must contain a main method with the following header (signature):
public static void main(String[] args)
main method in Java
However, in Java 25 it became possible to write the main() method in a short form (in Java 21–24 it was available only as a preview feature).
So every time you write code like:
void main()
{
Scanner console = new Scanner(System.in);
int age = console.nextInt();
System.out.println("Age: " + age);
}
The Java compiler “wraps” such code in a class on its own. In simplified form, it looks like this:
import module java.base; // implicitly: everything from java.base is available without import
final class Program { // the name comes from the file name: Program.java
void main() { // an instance method, not static and without String[]
Scanner console = new Scanner(System.in);
int age = console.nextInt();
System.out.println("Age: " + age);
}
}
4. A typical Java class
A typical Java class looks like this:
public class MathUtils
{
// Static variables (global for the class)
static final double PI = 3.14159;
static int callCounter = 0;
// Static method: compute the area of a circle
public static double circleArea(double radius)
{
callCounter++;
return PI * radius * radius;
}
// Static method: find the maximum of two numbers
public static int max(int a, int b)
{
callCounter++;
return (a > b) ? a : b;
}
// Static method: how many times methods were called
public static int getCallCount()
{
return callCounter;
}
// Access fields and methods from the main() method
public static void main(String[] args)
{
System.out.println("Circle area: " + circleArea(5));
System.out.println("Maximum: " + max(10, 42));
System.out.println("Methods were called " + getCallCount() + " time(s)");
}
}
What do we see here:
- A class can have many methods.
- A class can have its own variables.
- Class methods can call each other.
- Class methods can access shared variables.
- Before each method there are the same “magic words” like: public static.
- Before each method there are different types: int, double, void.
We’ll go through all these nuances below.
5. Class fields (variables) are its memory
Inside a class you can store variables, which are often called fields. They’re like memory cells where we put some data.
public class MathUtils
{
// Static variables (global for the class)
static final double PI = 3.14159;
static int callCounter = 0;
}
Fields let a class remember something between calls to different methods. For example, count how many times a button was pressed. Or how many times a method was called (callCounter). There can be as many fields as you like.
6. Methods — the actions a class can perform
Methods are the “skills” of our class. If the class is an employee, methods are its skills: what it can do.
For example, a method can:
- print something to the screen,
- compute the sum of two numbers,
- accept data as “input” and return a result.
Method structure:
- Return type (for example, void — if it returns nothing, or int — if it returns a number).
- Method name (for example, circleArea).
- Parameters in parentheses (can be absent).
- Curly braces { ... } — the method body.
public class MathUtils
{
// Static method: compute the area of a circle
public static double circleArea(double radius)
{
callCounter++;
return PI * radius * radius;
}
// Static method: find the maximum of two numbers
public static int max(int a, int b)
{
callCounter++;
return (a > b) ? a : b;
}
}
You’ll learn more about methods and all the nuances related to them in the upcoming lectures.
7. Access modifiers
In Java, you can define which parts of a class are accessible from other classes and which remain for internal use only. For this, we use access modifiers.
- public — visible to everyone. Such methods and fields can be used/called from anywhere in the program.
- private — visible only inside the class itself. These are internal details that shouldn’t be accessible to others. As if they don’t exist at all.
public class MathUtils
{
// Static variables (global for the class)
private static final double PI = 3.14159;
private static int callCounter = 0;
// Static method: compute the area of a circle
public static double circleArea(double radius)
{
callCounter++;
return PI * radius * radius;
}
}
The golden rule: fields are usually made private, and only the necessary methods with the public modifier are exposed. More on visibility modifiers — in the upcoming lectures.
8. Static methods and variables
Usually in Java, a class is used as a template for creating objects. You write your class, then create objects of this class using the new keyword, and only then call methods on the objects.
However, there’s another way to use a class — as a container for global variables and methods that can be called from anywhere in the program. This is exactly what the static keyword is for. It tells the Java compiler that the class’s methods and fields are bound not to an object of the class but to the class itself. Therefore, they can be called without creating any objects of the class.
You actually use such methods all the time:
Integer.parseInt(); // Static method parseInt() of the Integer class
String.valueOf(); // Static method valueOf() of the String class
Double.isInfinite(); // Static method isInfinite() of the Double class
Arrays.copy(); // Static method copy() of the Arrays class
The main() method is always declared static, since program execution starts from it — before any objects are created.
9. Tip in IDEA
The full signature of the main() method looks like this:
public static void main(String[] args)
And to avoid typing it every time in IntelliJ IDEA, there’s a shortcut. Just type psvm and press Tab, and IntelliJ IDEA will write the following code for you:
public static void main(String[] args)
{
}
It’s easy to remember — psvm — the first letters of public static void main.
GO TO FULL VERSION