This article focuses on the foundations of Java object-oriented programming. It explains how classes, objects, constructors, member variables, member methods, and access control work together, addressing the most common beginner questions: how objects are created, how methods are called, and how variables are accessed. Keywords: Java OOP, classes and objects, access modifiers.
Technical Specification Snapshot
| Parameter | Content |
|---|---|
| Language | Java |
| Protocol/Source | Reworked blog article content |
| Star Count | Not applicable |
| Core Dependencies | JDK, JVM, java.lang |
| Topic Focus | Classes, objects, methods, modifiers, constructors |
This article explains the minimum complete knowledge loop for Java OOP
The core topic of the original content is not a framework or library, but an introduction to Java OOP. It starts with modifiers and gradually moves into class declarations, object creation, method calls, constructors, parameter passing, and the JVM memory model, forming a learning path that works well for beginners.
For developers, the biggest pain point is usually not the syntax itself, but understanding what belongs to the class, what belongs to the object, when to use the class name, and when new is required. This reworked article uses a single Account example to explain those questions clearly.
Access modifiers determine who can use a member
Java modifiers fall into two categories: access modifiers and non-access modifiers. The access control set mainly includes public, protected, default access, and private. These directly determine the visibility of classes, methods, and member variables.
| Modifier | Same Class | Same Package, Different Class | Subclass in Different Package | Non-Subclass in Different Package |
|---|---|---|---|---|
| public | √ | √ | √ | √ |
| protected | √ | √ | √ | × |
| default | √ | √ | × | × |
| private | √ | × | × | × |
A few rules are especially important: protected and private cannot modify top-level classes. A .java file usually allows only one public top-level class, and the class name must match the file name.
public class Account {
private int id; // Private member variable: directly accessible only within the current class
private String name; // Private fields are typically used with getters/setters
private double balance;
}
This code demonstrates the most basic form of encapsulation: keep fields private and expose access through methods.
A class is composed of member variables, constructors, and member methods
A business class usually contains three parts: member variables that describe state, constructors that handle initialization, and member methods that define behavior. This is the basic structure of Java modeling.
A single Account class is enough to explain the core OOP actions
public class Account {
private int id;
private String name;
private double balance;
public Account() {
// No-argument constructor: allows the object to be created first and populated step by step
}
public Account(int id, String name, double balance) {
this.id = id; // this refers to the current object and is used to assign member variables
this.name = name;
this.balance = balance;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance = balance + amount; // Deposit: modify object state
}
public void withdraw(double amount) {
balance = balance - amount; // Withdraw: modify object state
}
}
This code fully shows how a class can express both data and behavior.
Object creation and method calls follow the rule: instances belong to objects, static members belong to classes
Instance variables and instance methods belong to objects. Static variables and static methods belong to the class. Once you understand this rule, you can clearly distinguish when to use objectName.method() and when to use ClassName.method().
The entry class creates objects and drives business execution
public class AccountDemo {
public static void main(String[] args) {
Account myAccount = new Account(1001, "Tina Wang", 2000);
myAccount.deposit(5000); // Call an instance method
myAccount.withdraw(3000); // Call an instance method
System.out.println(myAccount.getId());
System.out.println(myAccount.getName());
System.out.println(myAccount.getBalance());
}
}
This code shows a typical separation of responsibilities: the business class handles modeling, while the entry class handles execution.
this and super resolve object context
this represents the current object. It is commonly used to resolve naming conflicts between member variables and parameters, and it can also call another constructor in the same class. super is used by a subclass to access a parent constructor, method, or hidden member.
Note: neither this nor super can be used directly in a static method, because a static context is not attached to a specific object.
Method overloading, parameter passing, and reference semantics are common beginner pitfalls
Java supports method overloading, but the distinction is based on different parameter lists, not different return types. In other words, methods with the same name can legally coexist only if they differ in parameter count, type, or order.
Java parameter passing is always pass-by-value in essence
For primitive types, Java passes a copy of the value. For reference types, Java passes a copy of the reference. The copy and the original reference still point to the same object, so code inside the method can modify the object’s state, but it cannot change the binding of the external variable itself.
public class PassByValue {
public static void changeNumber(int num) {
num = 200; // Modifies the copy and does not affect the external variable
}
public static void changeAccount(Account account) {
account.setBalance(10000); // Modify internal state through the same object reference
}
}
This example reveals a common conclusion: Java does not have pass-by-reference semantics. It only passes reference values by value.
JVM memory regions help explain object lifecycle
From a beginner’s perspective, you can roughly understand JVM memory as the stack area, heap area, and method area. Local variables and method parameters are typically located on the stack. Objects are usually created on the heap. Class metadata and method bytecode are carried by areas related to the method area.
Garbage collection only handles unreachable objects
An object becomes eligible for garbage collection only when no references point to it anymore. Assigning one reference to another variable does not copy the object itself. It only copies the address relationship.
Account a1 = new Account();
Account a2 = new Account();
a2 = a1; // The object originally referenced by a2 becomes a candidate for unreachability
Account a3 = new Account();
a3 = null; // Explicitly remove the reference
This code shows when an object may lose reachability and enter the candidate set for garbage collection.
Modern Java also provides var to reduce boilerplate
When the type of a local variable can be clearly inferred from the right-hand side, you can use var to simplify declarations. However, it can only be used for local variables, and the variable must be initialized at the time of declaration.
var account = new Account(); // The compiler infers Account
var name = "Tina Wang"; // Inferred as String
The purpose of this feature is to reduce boilerplate, but it does not change Java’s statically typed nature.
Image metadata can help identify the publishing and sharing context

AI Visual Insight: This image shows a sharing guidance animation on a blog page, prompting users to share the content through the top-right menu entry. It does not involve Java implementation details, but it indicates that the original article was published on a content platform page rather than in a code repository or official documentation site.
The core value of this article is building a mental model for Java OOP
If you remember only one sentence, make it this: a class is a template, and an object is an instance; instance members belong to objects, static members belong to classes; encapsulation relies on access modifiers, initialization relies on constructors, and behavior is exposed through methods. Once you internalize this model, learning inheritance, polymorphism, and interfaces becomes much easier.
FAQ
1. Why must instance methods be called through an object?
Because instance methods depend on the state of a specific object. At runtime, Java must know which instance is being operated on. Static methods do not depend on object state, so they are typically called with the class name.
2. What is the essential difference between this and super?
this points to the current object and helps with member access and constructor reuse inside the current class. super represents the parent-class context and is used to access parent constructors, overridden methods, or hidden members.
3. When Java passes an object as a parameter, why do external changes appear after the method modifies it?
Because Java passes a copy of the reference value. Both the copied reference and the original reference still point to the same heap object. The method modifies the internal state of that object, not the external variable name itself.
Core summary: Using a Java OOP beginner example, this article systematically explains classes, objects, constructors, member variables, access modifiers, static/final/abstract, this/super, parameter passing, and the memory model. It also uses the Account example to show the typical structure of a business class and an entry class.