Create your own ‘Private Blockchain’

Here’s a use case of blockchains. Being case-specific, this use case will help you understand how Blockchains can revolutionize all verticals in the marketplace. The use case centers around devising lightweight financial systems using blockchains.

Check this Blockchain video by Intellipaat

Use Cases of Blockchains

Youtube subscribe

Within this financial system, one or more scarce assets are transacted and exchanged between entities that are part of the system. For any asset to remain scarce, two related problems need to be solved.

  • First, we must ensure that the same unit of the asset cannot be ‘double spent’.
  • Second, we must make sure that no one can create new units through forgery. Although physical tokens resolve these issues, they trivially solve the problem of double spending, as the rules of basic physics prevent one token from being in two places at the same time.

The problem of forgery is solved by making the process of manufacturing tokens formidable. However, they do not turn out to be efficient all the time. We can make systems efficient by leaving physical tokens behind and redefining asset ownership in terms of a distributed ledger. Here, enters the blockchain.
In the blockchain, each entity runs a ‘node’ that holds a copy of the ledger and maintains full control over its own assets, which are further protected by encrypted private keys. Transactions propagate between nodes in a peer-to-peer network, with the blockchain ensuring that consensus is maintained. This architecture eliminates central attack points. As a result, deploying a digital financial system becomes fast and cost-effective with real-time automatic reconciliation.
Some popular examples of lightweight financial systems are crowdfunding, gift cards, loyalty points, and local currencies—especially in cases where assets are redeemable at multiple places with multiple users.
In all these cases, the lower cost and friction of blockchains provides an immediate benefit, while the loss of confidentiality is not a concern.

Learn about Blockchain technology from the Intellipaat Blockchain training course and excel in your career as a Blockchain Developer!

Below is a code that will help you build your own private blockchain. Though the code is basic, it will clear your concerns on creating blockchains in Java.

To facilitate a better understanding of the concept, we have developed the code and explained the classes that are used. Do go through this module to code better with blockchains.
First, there is a need for us to be on the same page about what blockchain is and how it works before we attempt to code them ourselves. The block has header information and a set or a “block” of data. This data is a transaction that can be adapted. The chain then starts with a Genesis block or the first block within the blockchain, and further new blocks are created depending on how many transactions or sets of data are stored within a block.
Once the threshold is reached, a new block is created and added to the chain. This new block is connected to the previous one with a unique hashing mechanism, making blockchains immutable. The hash is a SHA-256 code that is involved with every transaction or set of data. The content within the block is also hashed, meaning that there is a unique identifier for every block.

Blockchain masters program

We have divided the code into three parts, i.e., we have dedicated three Java Classes to execute the code.

package jobs;
import java.util.Date;
public class Block {
public String hash;
public String previousHash;
private String data;
private long timeStamp;
//Block Constructor.
public Block (String data,String previousHash ) { = data;
this.previousHash = previousHash;
this.timeStamp = new Date().getTime();
this.hash = calculateHash();
public String calculateHash() {
String calculatedhash = StringUtil.applySha256(
previousHash +
Long.toString(timeStamp) +
return calculatedhash;

Here’s the first class ‘Block’. The class Block contains four class variables that comprise block information including the hash code, previousHash, data, and timestamp. Depending on security and privacy requirements, the access specifiers of class variables are kept public or private.
Skimming further through the class, we can find a constructor. This parameterized constructor, which takes in data and previousHash as its parameters, is used to assign values to the class variables. For getting the timestamp, we are using the library function getTime() which is already available in the Java.util package. Further, we also have a function calculateHash() to compute the hash values of individual blocks. The variable calculatedHash of calculateHash() stores the calculated hash value and is then returned at the end of the function.

package jobs;
public class StringUtil {
//Applies Sha256 to a string and returns the result.
public static String applySha256(String input){
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//Applies sha256 to our input
byte[] hash = digest.digest(input.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
return hexString.toString();
catch(Exception e) {
throw new RuntimeException(e);

In this block of statements, we have written codes inside the class ‘StringUtil’. In this class, we apply the inherent SHA-256 code to a string and return the result. Fortunately, Java has a library function ‘MessageDigest’ that applies SHA-256 code to our input. This will generate a unique hash value. However, the hash value will be in hexadecimal format. Therefore, we need to convert that hexadecimal hash function to a string. We explicitly convert the hash function to a string and write a code to ensure that whenever a runtime exception arises, it will be thrown.

package jobs;
import java.util.ArrayList;
public class Main {
public static ArrayList blockchain = new ArrayList();
	public static void main(String[] args) {
		//add the blocks to the blockchain ArrayList:
		blockchain.add(new Block("Hi im the first block", "0"));
		blockchain.add(new Block("Yo im the second block",blockchain.get(blockchain.size()-1).hash));
		blockchain.add(new Block("Hey im the third block",blockchain.get(blockchain.size()-1).hash));
		String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);

The third and final ‘Main’ class is where the blockchain is created. Here, we import the GsonBuilder function to add new blocks to the blockchain. Each block is sent to output and their details are printed on the screen. As new blocks keep on getting added to the blockchain, their details are displayed as output every time the program gets compiled and executed. The main() function contains all the codes that are required to display the details and add new blocks to our blockchain.
Here’s what the output of our code looks like. When we execute the main() function in Eclipse, we see the details of the three blocks. The details include individual hash codes, previous hash values, data, and timestamps of each block that is present and added in due course of time.

Recommended Videos

Course Schedule

Name Date
Cloud Architect 2021-01-23 2021-01-24
(Sat-Sun) Weekend batch
View Details
Cloud Architect 2021-01-30 2021-01-31
(Sat-Sun) Weekend batch
View Details
Cloud Architect 2021-02-06 2021-02-07
(Sat-Sun) Weekend batch
View Details

Leave a Reply

Your email address will not be published. Required fields are marked *