Java Objects from Scratch: A Step‑by‑Step Guide
This tutorial walks through the fundamentals of Java objects, covering declaration, instantiation, referencing, practical code examples—including a HelloTest class and a Point/Rectangle demo—and explains Java's automatic garbage collection and manual cleanup techniques.
Creating Objects
Object creation in Java consists of three steps: declaration, instantiation, and referencing.
1.1 Declare an object ClassName objectName; Example:
Car c1, c2; // declare two Car objects
Float f1, f2; // declare two Float objectsAfter declaration, no memory is allocated and the object cannot be used.
1.2 Instantiate an object
objectName = new ClassConstructor([actual parameters]);Key points:
The new operator allocates memory and invokes the constructor, returning a reference.
The constructor name is the same as the class name.
Actual parameters (arguments) must match the constructor’s signature; parentheses cannot be omitted even if there are no arguments.
Example:
c1 = new Car("Red", "Black");
f1 = new Float(10f);Declaration and instantiation can be combined:
ClassName objectName = new ClassConstructor([actual parameters]);Example:
Car c1 = new Car("Red", "Black");
Float f1 = new Float(10f);1.3 Reference an object
After an object is created, its fields and methods are accessed via the reference using the dot operator ( .).
objectName.fieldName;
objectName.methodName(actualParameters);Example of correcting a field after creation:
Car c2 = new Car("Great Wall", "Rainbow");
// Fix the color typo
c2.color = "Red";Simple HelloTest Example
public class HelloTest {
public static void main(String[] args) {
// Declare a Hello instance
Hello hello = new Hello();
// Call the sayHello method three times
hello.sayHello();
hello.sayHello();
hello.sayHello();
}
}
class Hello {
// Member method
void sayHello() {
System.out.println("Hello!");
}
}Program output:
Hello!
Hello!
Hello!Object Usage with Point and Rectangle
The following demo defines two classes, Point and Rectangle, and shows how to create and manipulate their instances.
public class Point {
public int x = 0; // x coordinate
public int y = 0; // y coordinate
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Rectangle {
public int width = 0;
public int length = 0;
public Point origin;
// Default constructor
public Rectangle() {
origin = new Point(0, 0);
}
// Constructor with a Point
public Rectangle(Point p) {
origin = p;
}
// Constructor with width and length
public Rectangle(int w, int l) {
origin = new Point(0, 0);
width = w;
length = l;
}
// Constructor with Point, width, and length
public Rectangle(Point p, int w, int l) {
origin = p;
width = w;
length = l;
}
public void move(int newX, int newY) {
origin.x = newX;
origin.y = newY;
}
public int getArea() {
return width * length;
}
}
public class ObjectOperationTest {
public static void main(String[] args) {
Point p = new Point(11, 22);
Rectangle rect1 = new Rectangle(p, 20, 10);
Rectangle rect2 = new Rectangle(20, 10);
System.out.println("rect1 width: " + rect1.width);
System.out.println("rect1 length: " + rect1.length);
System.out.println("rect1 area: " + rect1.getArea());
rect2.origin = p;
System.out.println("rect2 X: " + rect2.origin.x);
System.out.println("rect2 Y: " + rect2.origin.y);
rect2.move(30, 40);
System.out.println("rect2 new X: " + rect2.origin.x);
System.out.println("rect2 new Y: " + rect2.origin.y);
}
}Program output:
rect1 width: 20
rect1 length: 10
rect1 area: 200
rect2 X: 11
rect2 Y: 22
rect2 new X: 30
rect2 new Y: 40Object Cleanup
Java automatically reclaims memory of unreachable objects via its garbage collector. Developers can optionally request a collection with System.gc(), use finalize() (though it is discouraged), or nullify references to make objects eligible for collection.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
