Blockchain in Java Example

In this tutorial, we will learn about Blockchain technology and its implementation in Java.

Blockchain is a record keeping technology in which data are stored in a collection of blocks that are linked with each other. The data in each block contains a timestamp, transactions information, and a cryptographic hash of both the previous and the current data. Every block has a copy of information about the block previous to it, in the form of a cryptographic hash value. This makes Blockchain resistant proof to alteration of data once it is recorded. The data in one block cannot be modified without updating all subsequent blocks. Every time a new data comes in, it is stored in a new block and chained onto the previous block. Any kind of information can be stored in Blockchain but the most common use so far has been for keeping transactions information.

Blockchain technology is used in a decentralized manner so that no individual, company, groups or organization can have control over the data, instead every user has control over their data.

Blockchain was invented by Satoshi Nakamoto in 2008.

Implementing Blockchain in Java

  1. Create a simple POJO class to represent the data of a block. The following code shows how to do so:
    import java.util.Arrays;
    public class Block {
        private String[] transaction;
        private long timestamp;
        private String previousBlockHash;
        private String blockHash;
        public Block(String[] transaction, long timestamp, String previousBlockHash) {
            this.transaction = transaction;
            this.timestamp = timestamp;
            this.previousBlockHash = previousBlockHash;
            this.blockHash = CryptoManager.convertToHash(previousBlockHash + timestamp + transaction);
        public String[] getTransaction() {
            return transaction;
        public void setTransaction(String[] transaction) {
            this.transaction = transaction;
        public long getTimestamp() {
            return timestamp;
        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        public String getPreviousBlockHash() {
            return previousBlockHash;
        public void setPreviousBlockHash(String previousBlockHash) {
            this.previousBlockHash = previousBlockHash;
        public String getBlockHash() {
            return blockHash;
        public void setBlockHash(String blockHash) {
            this.blockHash = blockHash;
        public String toString() {
            return "Block [transaction=" + Arrays.toString(transaction) + ", timestamp=" + timestamp
                    + ", previousBlockHash=" + previousBlockHash + ", blockHash=" + blockHash + "]";
  3. Create a static method to convert your transaction data into hash:
    import java.nio.charset.StandardCharsets;
    public class CryptoManager {
        public static final String convertToHash(String data) {
            try {
                MessageDigest digest = MessageDigest.getInstance("SHA-256");
                byte[] bytes = digest.digest(data.getBytes(StandardCharsets.UTF_8));
                StringBuffer hexBuffer = new StringBuffer();
                //Converting each character to a hexadecimal string
                for (byte b : bytes) {
                    hexBuffer.append(String.format("%02x", b));
                return hexBuffer.toString();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
  5. Now, lets create blocks with transaction information and store them in an ArrayList as shown in the example below:
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    public class BlockchainExample {
        public static void main(String[] args) {
            List<Block> blockChain = new ArrayList<>();
            String[] firstTransaction = {"A has $20","A gives B $5"};
            Block firstBlock = new Block(firstTransaction, new Date().getTime(), "0");
            System.out.println("First block = " + firstBlock.toString());
            System.out.println("Blockchain = " + blockChain.toString());
            String[] secondTransaction = {"A has $15","A gives C $5"};
            Block secondBlock = new Block(secondTransaction, new Date().getTime(),
            System.out.println("Second block = " + secondBlock.toString());
            System.out.println("Blockchain = " + blockChain.toString());
            String[] thirdTransaction = {"A has $10","A gives D $5"};
            Block thirdBlock = new Block(thirdTransaction, new Date().getTime(),
            System.out.println("Third block = " + thirdBlock.toString());
            System.out.println("Blockchain = " + blockChain.toString());
            String[] fourthTransaction = {"A has $5","A gives E $2"};
            Block fourthBlock = new Block(fourthTransaction, new Date().getTime(),
            System.out.println("Fourth block = " + fourthBlock.toString());
            System.out.println("Blockchain = " + blockChain.toString());