Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninjas!, Welcome back.
We all use desktop applications in our daily life. When you have to do a lot of calculations, you directly open the calculator app. When you have to write some notes, you directly open the Notepad app. Similarly, If you want to get a reminder, you quickly set up an alarm using the Alarm app. These all are nothing but examples of Java Standalone applications.
In this article, titled “Java Standalone Application”, we will discuss what standalone applications are and how we can make “Java Standalone Application” by taking a simple example of a daily expense tracker app.
As the name suggests, a “Java Standalone Application” is an application that can stand on its own. In other words, It does not require any other resource for its execution.
“Java Standalone Application” does not require the Internet.
It runs on a local machine. It takes its input from the local machine as well saves its data to that local machine. Apart from the Internet, “Java standalone application” does not require any other process for its functionality.
Below are mentioned features of Java Standalone application:
Installation: Java Standalone Application requires Installation. Using them through a browser like we use web applications is impossible.
Self-contained: Java Standalone Applications are self-contained. They contain all libraries and code files required to run the application. They run smoothly and don’t have any external dependencies.
Offline operation: Java Standalone Application works in offline mode. They don’t require any kind of network connectivity for their functioning.
User-Interface: Generally, Java Standalone Applications have Graphical User Interface. It is not mandatory, though; we can create command-line standalone applications, also.
Data Storage: They store data on the local machine, making it easy to access and less vulnerable to attacks.
Performance and Security: Java Standalone applications work more smoothly than web apps since they don’t require connectivity or other process dependencies. Their code is not exposed to the web, which makes them more secure.
How to create a Java Standalone Application?
The following are steps to create a Java Standalone Application:-
1. Create a logic file: First, you must create a Java source code file containing your business logic. In our example, the Java source code file will contain code for the expense tracker.
Following is our source code file ExpenseTracker.java :
import java.util.ArrayList;
import java.util.Scanner;
public class ExpenseTracker {
/*Record class object will contain record of each transaction
* money will store the amount used in transaction ,-ve money stands for expense , +ve stands for income
* recordInfo will store information about that string
* for example , money = 200 , recordInfo = barber shop. */
static class Record{
int money;
String recordInfo;
// constructor for Record object
Record(int money, String recordInfo){
this.money = money;
this.recordInfo = recordInfo;
}
// helpful in case we want to print record object
@Override
public String toString() {
return "Money : "+money+"\n"+"record Info: "+recordInfo+".";
}
}
// will store all records
static ArrayList<Record> recordList = new ArrayList<>();
// will store total balance , either +ve or -ve
static int totalBalance;
private static int getTotalBalance() {
return totalBalance;
}
/* add a new record in recordList and update the total balance*/
private static void addRecord(Record r){
recordList.add(r);
totalBalance += r.money;
}
/* remove the said record from recordList and update the total balance */
private static boolean removeRecord(int i){
try{
Record removed = recordList.remove(i);
totalBalance -= removed.money;
}
catch (Exception e){
return false;
}
return true;
}
/* print all the record , one by one*/
static void printRecords(){
for(Record r : recordList){
System.out.println(r);
}
System.out.println();
}
/* helper fn to print choice list */
private static void printMainFn(){
System.out.println("Hi... I'm your expense manager");
System.out.println("Choose one out of following option :");
System.out.println("1. add a record");
System.out.println("2. delete a record");
System.out.println("3. view all records");
System.out.println("4. Tell me my balance");
System.out.println("5. exit");
System.out.println("Type 1 for option 1 and so on ...");
}
public static void main(String[] args) {
/* Scanner for taking input from user*/
Scanner sc = new Scanner(System.in);
// run loop until user exit out by choosing option 5
while (true) {
// print choice list
printMainFn();
// ask for user choice
String choice = sc.nextLine();
if (choice.equals("1")) {
/* user want to add a new record*/
System.out.println("add a new record");
System.out.println("In first line add amount. +ve for income , -ve for expense");
System.out.println("In second line, give an information statement about it");
/* take amount of transaction input from user in string form*/
String moneyInStringForm = sc.nextLine();
/* convert that amount to Integer form*/
int money = Integer.parseInt(moneyInStringForm);
sc.nextLine();
/* ask user for statement about transaction */
String statement = sc.nextLine();
System.out.println("statement is : "+statement);
// make a new record
Record newRecord = new Record(money,statement);
addRecord(newRecord);
System.out.println("Updated total balance is ... "+getTotalBalance());
} else if (choice.equals("2")) {
/* user want to delete a record , print all record so user can decide*/
System.out.println("Following are all records , choose a record index (0-based) to delete");
/* ask user record it want to delete*/
String toDeleteIndexInStringForm = sc.nextLine();
int toDeleteIndex = Integer.parseInt(toDeleteIndexInStringForm);
/* delete the said record*/
boolean isDeleted = removeRecord(toDeleteIndex);
if(isDeleted){
System.out.println("Record is deleted ...");
System.out.println("Updated balance is "+getTotalBalance());
}
else{
/* can not delete the record, may be due to wrong index*/
System.out.println("fail to remove the record ...");
}
} else if (choice.equals("3")) {
/* print main choice list */
printRecords();
} else if (choice.equals("4")) {
/* print total balance*/
System.out.println("Your balance is "+getTotalBalance());
} else if (choice.equals("5")) {
/* user choose to exit from app*/
break;
} else {
/* user give wrong input*/
System.out.println("Wrong input , again type correct input");
}
System.out.println();
sc.nextLine();
}
}
}
You can also try this code with Online Java Compiler
Java source code file is first compiled using java Filename.java command. It will create a separate .class file for each class present in Filename. Then we can java Filename.class command to run our file.
What are the advantages of Java Standalone applications over web-based applications?
Java Standalone applications have better performance than web-based applications. They don’t require internet connectivity. They are fast. Since they store data on local machines, they can be used for applications where data security is very important.
What are GUI applications and how can we create them using Java?
A graphical user interface (GUI) application contains different visual features such as windows, menus, and checkboxes. These graphic features make the application more interactive and easier for the user. We can use libraries like JavaFx and Swing to make GUI applications using Java.
Conclusion
In conclusion, Java standalone applications can work on their own. Neither they require connectivity to the Internet nor support from other applications for their functionality. They run on local machines, which makes them less vulnerable to attacks. This also makes them function smoothly. But this also makes it non-scalable. They are less flexible, and they don’t have the ability to share resources among people. Hence, we should use standalone applications keeping advantages and disadvantages in our mind.