Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.You can define an enumeration using the keyword enum followed by the name of the enumeration as −enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Just like arrays, the elements/constants in an enumeration are identified using numbers starting from 0 in the above example the days are identified using numbers as shown in the following illustration −Integers as elements of an enumNo, we can have ... Read More
In programming to hold data members we use variables, Java you can declare three types of variables namely, Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class (static) variables − Class variables are variables declared within ... Read More
There are several logging frame works available to log your data into files. You can also define your own method. In either cases to add the current time to your logged exception you can use the LocalDateTime class.It is an immutable class representing the date-time, it stores date-time as year-month-day-hour-minute-second. The now() method of this class returns the current date-time.Using this method concatenate the current date and time to your exception message and write to your required file.Exampleimport java.io.FileOutputStream; import java.io.IOException; import java.time.LocalDateTime; import java.util.Arrays; import java.util.Scanner; public class LoggingToFile { private static void writeLogToFile(Exception e) throws IOException { ... Read More
A tetrahedron is a pyramid with triangular base i.e. it has a base that is a triangle and each side has a triangle. All the three triangles converge to a point. As in the figure, Area of Tetrahedron = (√3)a2ExampleThe code to find the area of tetrahedron uses the math library to find the square and square-root of a number using sqrt and pow methods. For calculating the area we take a floating point and the value of the expression "((sqrt(3)*a*a))" is given to it.#include #include int main() { int a= 5; float area, volume; ... Read More
DFA stands for Deterministic Finite Automata. It is a finite state machine that accepts or a string based on its acceptor.Here, we are going to make a DFA that accepts a string that starts and ends with a. The input is from the set (a, b). Based on this we will design a DFA. Now, Let's discuss some valid and invalid cases that are accepted by a DFA.Strings that are accepted by DFA: ababba, aabba, aa, a.Strings that are not accepted by DFA: ab, b, aabab.ExampleThis program check for a string that starts and ends with a. This DFA will ... Read More
The String type is a class in Java, it is used to represent a set of characters. Strings in Java are immutable, you cannot change the value of a String once created.Since a String is immutable, if you try to reassign the value of a String. The reference of it will be pointed to the new String object leaving an unused String in the memory.Java provides StringBuffer class as a replacement of Strings in places where there is a necessity to make a lot of modifications to Strings of characters.You can modify/manipulate the contents of a StringBuffer over and over ... Read More
A class with in another class is known as inner class, you cannot declare a class static unless it is an inner class. A static inner class is just like other class variables. You can access it (static inner class) without instantiationExampleYou can access the static variable of an outer class just using the class name. Following Java example demonstrates how to access static variables of a class from a static inner class.public class Outer { static int data = 200; static class InnerDemo { public void my_method() { System.out.println("This is ... Read More
An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.A static method is declared using the static keyword and it will be loaded into the memory along with the class. You can access static methods using class name without instantiation.Static methods in an interface since java8Since Java8 you can have static methods in an interface (with body). You need to call them using the name of the interface, just like static methods of a class.ExampleIn the following example, we are defining a static method in an interface and accessing ... Read More
The only possible solution is to get the stack trace of the current thread. Get the class name using an element in the stack trace. Pass it to the forName() method of the class named Class.This returns a Class object and you can get an instance of this class using the newInstance() method.Examplepublic class MyClass { String name = "Krishna"; private int age = 25; public MyClass() { System.out.println("Object of the class MyClass"); System.out.println("name: "+this.name); System.out.println("age: "+this.age); } public static void demoMethod() throws Exception { ... Read More
Whenever you return values from a static method they are either static nor instance by default, they are just values.The user invoking the method can use them as he wants. i.e. you can retrieve the values and declare them static.But, since you cannot declare variables of a method static if you need to declare the vales returned by a method static you need to invoke it in the class outside the methods.ExampleAssume we have a class with name Demo as −class Demo{ int data = 20; public Demo(int data){ this.data = data; } ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP