Java RMI adding numbers
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A simple "add two numbers" example is the classic way to learn Java RMI because it shows the full remote-call path without distracting business logic. The point is not the arithmetic. The point is understanding how a client invokes a method on an object living in another JVM through a remote interface, a registry lookup, and serialized request-response handling.
Start with a Remote Interface
Every RMI service begins with a remote interface that extends Remote. Each remotely callable method must declare RemoteException.
This interface is the contract shared by client and server. The client does not need the server implementation class, but it does need this interface so it can invoke the remote method safely.
Implement the Remote Object
The server-side implementation usually extends UnicastRemoteObject.
For this example, the remote logic is intentionally trivial. That is useful because it keeps the network mechanics visible.
Bind the Service in the Registry
The server must create an RMI registry or connect to one and bind the remote object under a name.
Using rebind is often easier during development because it replaces an existing binding if the service name was already registered.
In a distributed setup, the registry may run as a separate process or on another host. The idea stays the same: the server publishes a name, and the client resolves that name.
Look Up and Call the Service from the Client
The client contacts the registry, looks up the remote object, casts it to the shared interface, and calls the method as if it were local.
That local-looking call is the key idea behind RMI. Under the hood, Java handles marshalling, transport, and unmarshalling for you.
Understand What Actually Happens on the Network
Even though the client code looks simple, the call is remote. That means:
- latency is real
- connection failures are possible
- serialization rules still matter
- '
RemoteExceptionis not optional boilerplate'
This is why remote methods should be designed as remote methods, not as ordinary in-process helpers. In the addition example the cost is invisible, but in real systems it shapes API design.
Compile and Run in a Predictable Order
A basic manual flow looks like this:
In modern Java versions, explicit stub generation is usually unnecessary for ordinary dynamic stub handling. Older RMI tutorials often include extra rmic steps, which can confuse readers working with newer JDKs.
That is one reason simple, updated examples are valuable: the core concept survives, but some historical boilerplate no longer does.
Common Pitfalls
- Forgetting that remote methods must declare
RemoteException. - Binding the service on the server but looking up a different name on the client.
- Treating remote calls as if they had the same cost and reliability as local method calls.
- Following outdated tutorials that assume manual stub-generation steps still apply universally.
- Sharing only the implementation class and forgetting that client and server both need the remote interface contract.
Summary
- Java RMI exposes remote methods through an interface that extends
Remote. - The server implements the interface and binds the object in an RMI registry.
- The client looks up the service by name and calls it through the shared interface.
- '
RemoteExceptionis part of the contract because network calls can fail.' - The addition example is simple by design, so the remote invocation flow stays visible.
Related reading
- Java Round up Any Number
- Java Runtime.getRuntime getting output from executing a command line program
- Java SafeVarargs annotation, does a standard or best practice exist?
- Java SE 6 vs. JRE 1.6 vs. JDK 1.6 - What do these mean?
- Java Security Illegal key size or default parameters?
- Java Serializable Object to Byte Array
- Java server connect to other server using sockets
- Java Set retain order?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.