Hey everyone!
In my previous post, I shared my journey and goals as I work towards becoming a Java Developer. Now, it’s time to dive into actual coding!
Java is an object-oriented programming (OOP) language, meaning everything revolves around objects. But why is this important? Think of it like math: you can’t solve logarithms without first understanding basic operations like addition and subtraction. The same applies to Java—before building complex applications, you must grasp the fundamentals.
Today, we’ll cover three core concepts in Java: objects, methods, and fields. We’ll also learn how to display output in the console using System.out.print()
. If you're just starting out, this post will give you a solid foundation. And if you already know Java, consider it a refresher
Objects in Java
In Java, we create objects using the class
keyword. A class serves as a blueprint for creating objects. Think of it like this:
- A Car is a class because it represents a general category.
- Specific cars like BMW, Volvo, or Toyota are objects that belong to this class
Every car has properties such as model name, number of seats, max speed, and color. In Java, these properties are called fields (variables).
Declaring Fields (Variables) in Java
Here’s how we define different types of variables in Java:
-
Integer Numbers (Whole Numbers):
-
byte
(from -128 to 127) -
short
(from -32,768 to 32,767) -
int
(from -2,147,483,648 to 2,147,483,647) -
long
(from -9 quintillion to +9 quintillion)
-
-
Decimal Numbers (Floating-Point Numbers):
float
double
-
Single Character:
char
-
Boolean Values (true/false):
boolean
-
Text (Strings):
- String (text enclosed in double quotes " ")
Let's create a simple Car
class with some fields:
class Car {
String brandName = "BMW";
String model = "M5";
int maxSpeed = 250;
double length = 4.983;
boolean isEngineRunning = false;
}
Understanding Field Declaration
Each field declaration consists of:
- A data type (String, int, double, etc.)
- A variable name (brandName, model, etc.)
- An assignment operator = (used to assign values)
- A semicolon ; (required by Java syntax to end a statement)
Rules for Naming Fields in Java
- Can contain letters (A-Z, a-z), numbers (0-9), underscores _, and dollar signs $ (though $ is rarely used in practice)
- Cannot start with a number (e.g., 3speed ❌)
- Cannot contain spaces (e.g., max speed ❌)
- Cannot use Java keywords (e.g., int, class, etc.)
- Follows camelCase convention (e.g., brandName, isEngineRunning)
How to Output Information in Java
To display information in the console, we use System.out.print()
and System.out.println()
:
-
System.out.print()
→ Prints text without moving to a new line. -
System.out.println()
→ Prints text and moves to the next line. Example:
System.out.println("Hello");
System.out.print("World!");
Output:
Hello
World!
We can also print variables:
String brand = "BMW";
System.out.println("Car brand: " + brand);
Output:
Car brand: BMW
The +
operator is used to concatenate (join) text and variables.
Methods in Java
Objects don’t just store data—they also perform actions! These actions are defined using methods.
For example, a car can start the engine, accelerate, or brake. In Java, we define these actions as methods inside the class.
Creating a Method
class Car {
String brandName = "BMW";
String model = "M5";
int maxSpeed = 250;
double length = 4.983;
boolean isEngineRunning = false;
void printCarInfo() {
System.out.println("Brand name: " + brandName);
System.out.println("Model: " + model);
System.out.println("Max Speed: " + maxSpeed);
System.out.println("Length: " + length);
System.out.println("Engine Running: " + isEngineRunning);
}
}
To define a method, use the following syntax:
[ReturnType] methodName() {
// method body
}
The Main Method: The Entry Point
A Java application requires a main method as its entry point. Java starts execution from the main method.
class Car {
String brandName = "BMW";
String model = "M5";
int maxSpeed = 250;
double length = 4.983;
boolean isEngineRunning = false;
public static void main(String[] args) {
Car car = new Car();
car.printCarInfo();
}
void printCarInfo() {
System.out.println("Brand name: " + brandName);
System.out.println("Model: " + model);
System.out.println("Max Speed: " + maxSpeed);
System.out.println("Length: " + length);
System.out.println("Engine Running: " + isEngineRunning);
}
}
Output:
Brand name: BMW
Model: M5
Max Speed: 250
Length: 4.983
Engine Running: false
Modifying Object Fields with Methods
We can also modify object properties using methods.
class Car {
String brandName = "BMW";
String model = "M5";
int maxSpeed = 250;
double length = 4.983;
boolean isEngineRunning = false;
public static void main(String[] args) {
Car car = new Car();
car.printCarInfo();
car.changeValues();
car.printCarInfo();
}
void printCarInfo() {
System.out.println("\nBrand name: " + brandName);
System.out.println("Model: " + model);
System.out.println("Max Speed: " + maxSpeed);
System.out.println("Length: " + length);
System.out.println("Engine Running: " + isEngineRunning);
}
void changeValues() {
brandName = "Mercedes";
model = "GLE";
maxSpeed = 230;
length = 4.926;
isEngineRunning = true;
}
}
Output:
Brand name: BMW
Model: M5
Max Speed: 250
Length: 4.983
Engine Running: false
Brand name: Mercedes
Model: GLE
Max Speed: 230
Length: 4.926
Engine Running: true
Summary
Today, we covered:
✅ Objects: The building blocks of Java programs
✅ Fields (Variables): Storing data inside objects
✅ Methods: Defining actions for objects
✅ Printing Output: Using System.out.print() and System.out.println()
✅ The Main Method: The starting point of a Java program
What’s Next?
In the next post, we'll dive deeper into constructors, a special type of method that helps us create objects efficiently!
Final Thoughts
Mastering Java starts with understanding the basics. I hope this post gave you a clear introduction to objects, fields, methods, and printing output in Java. If you have any questions, feel free to ask in the comments!
In order to better learn the material, I advise you to write your own class that would contain the main method, several fields of different types, and methods for output and changing these fields
Top comments (0)