OOP in Java
OOP in Java
Final Keyword
public class FinalExample {
public static void main(String[] args) {
final int constantValue = 42;
System.out.println("The constant value is: " + constantValue);
constantValue = 24;
}
}
Reflection
public class Main {
public static void main(String[] args){
System.out.println("String class info: " + String.class);
}
}
Collection
import java.util.ArrayList;
import java.util.List;
arr.add("Java");
arr.add("Python");
System.out.println(arr.get(0));
System.out.println(arr.get(1));
}
}
The Collection in Java is a framework that provides an architecture to store and manipulate the group of
objects. Java Collections can achieve all the operations that you perform on a data such as searching,
sorting, insertion, manipulation, and deletion. Java Collection means a single unit of objects. Java Collection
framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList).
Multi-Threading
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(" Value " + i);
}
}
}
RMI
RMI is a Java API that allows methods of objects in one Java Virtual Machine (JVM) to be invoked from
another JVM, making it possible to create distributed applications.
Stub (Client Side):
1. It is like the client's representative.
2. It takes requests, sends them to the remote object, and returns the results.
import java.rmi.*;
import java.rmi.server.*;
public class SearchQuery extends UnicastRemoteObject implements Search{
SearchQuery() throws RemoteException
{
super();
}
public String query(String search) throws RemoteException {
String result;
if (search.equals("Reflection in Java"))
result = "Found";
else
result = "Not Found";
return result;
}
}
import java.rmi.*;
public interface Search extends Remote
{
String query(String search) throws RemoteException;
}
Web Api’s
In Java, a Web API (Application Programming Interface) typically refers to a set of protocols and tools for
building web applications. Web APIs enable different software systems to communicate with each other
over the web. They allow developers to create, access, and integrate various functionalities and services in
their applications.
Servlets: Java Servlets are the foundation of many Java-based web applications. They manage HTTP
requests and responses, making them suitable for building the backend of a web API.
JAX-RS (Java API for RESTful Web Services): JAX-RS is a set of APIs and annotations for building
RESTful web services in Java. It simplifies the development of RESTful APIs by providing a standard
way to map Java methods to HTTP methods.
Jersey: Jersey is a popular implementation of the JAX-RS specification. It provides a framework for
building RESTful web services in Java.
Instance Initializer
public class Dog {
int instanceVariable;
{
System.out.println("Instance initializer block is executed");
instanceVariable = 10;
public Dog() {
System.out.println("Constructor is executed");
}