Java Program to Create an enum class In this example, we will learn to create an enum class in Java. Example 1: Java program to create an enum class enum Size{ // enum constants SMALL, MEDIUM, LARGE, EXTRALARGE; public String getSize(){ // this will refer to the object SMALL switch(this) { case SMALL: return “small”; …
Month: May 2021
Java Program to Determine the class of an object In this example, we will learn to determine the class of an object in Java using the getClass() method, instanceof operator, and the isInstance() method. Example 1: Check the class of an object using getClass() class Test1{ // first class } class Test2{ // second …
Java Program to Count number of lines present in the file In this example, we will learn to count the number of lines present in a file in Java. Example 1: Java program to count the number of lines in a file using Scanner class import java.io.File; import java.util.Scanner; class Main{ public static void main(String[] …
Java Program to Get the relative path from two absolute paths In this example, we will learn to get the relative path from two absolute paths in Java using String methods, URI class, and java.nio.file package. Example 1: Get a relative path from two absolute paths using URI class import java.io.File; import java.net.URI; class Main{ …
Java Program to Get the name of the file from the absolute path In this example, we will learn to get the name of the file from the absolute path in Java. Example 1: Get file name from the absolute path using getName() import java.io.File; class Main{ public static void main(String[] args){ // link to …