Found 2616 Articles for Java

What are the restrictions imposed on a static method or a static block of code in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:17:47

4K+ Views

static methods and static blocksStatic methods belong to the class and they will be loaded into the memory along with the class, you can invoke them without creating an object. (using the class name as reference).Whereas a static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class Sample {    static int num = 50;    static {       System.out.println("Hello this is a static block");    }    public static void ... Read More

How can we map multiple date formats using Jackson in Java?

raja
Updated on 06-Jul-2020 12:33:55

2K+ Views

A Jackson is a Java-based library and it can be useful to convert Java objects to JSON and JSON to Java Object. We can map the multiple date formats in the Jackson library using @JsonFormat annotation, it is a general-purpose annotation used for configuring details of how values of properties are to be serialized. The @JsonFormat has three important fields: shape, pattern,  and timezone. The shape field can define structure to use for serialization (JsonFormat.Shape.NUMBER and JsonFormat.Shape.STRING), the pattern field can be used in serialization and deserialization. For date, the pattern contains SimpleDateFormat compatible definition and finally, the timezone field can be used in serialization, default ... Read More

Can Enum extend any class in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:14:37

1K+ Views

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 −Enumerations are similar to classes and, you can have ... Read More

How to run a JAR file through command prompt in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:11:42

3K+ Views

For packaging of class files Java provides a file format known as JAR (Java Archive). Typically, a JAR file contains .class files, images, text files, libraries that are required to execute the application or, library.This file format is used to distribute application software and libraries in Java. All the predefined libraries are available in this format.If you have any library in this format to use it In your application either you need to place it in the current (or, lib) folder of the project or, you need to set the class path for that particular JAR file.Creating a Jar fileJava ... Read More

How many types of memory areas are allocated by JVM in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:06:04

1K+ Views

Java Virtual Machine is a program/software which takes Java bytecode (.class files) and converts the byte code (line by line) into machine understandable code.JVM contains a module known as a class loader. A class loader in JVM loads, links and, initializes a program. It −Loads the class into the memory. Verifies the byte code instructions.Allocates memory for the program.JVM memory locationsJVM has five memory locations namely −Heap − Runtime storage allocation for objects (reference types).Stack − Storage for local variables and partial results. A stack contains frames and allocates one for each thread. Once a thread gets completed, this frame also ... Read More

Why can't a Java class be both abstract and final?

Maruthi Krishna
Updated on 14-Oct-2019 08:04:43

3K+ Views

Abstract classA class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.If you want to use the concrete method in an abstract class you need to inherit the class, provide implementation to the abstract methods (if any) and then, you using the subclass object you can invoke the required methods.ExampleIn the following Java example, the abstract class MyClass contains a concrete method with name display.From another class (AbstractClassExample) we are inheriting the class MyClass and invoking the its concrete method display using the subclass ... Read More

How to read a .txt file with RandomAccessFile in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:00:58

1K+ Views

In general, while reading or writing data to a file, you can only read or, write data from the start of the file. you cannot read/write from random position.The java.io.RandomAccessFile class in Java enables you to read/write data to a random access file.This acts similar to a large array of bytes with an index or, cursor known as file pointer you can get the position of this pointer using the getFilePointer() method and set it using the seek() method.This class provides various methods to read and write data to a file. The readLine() method of this class reads the next ... Read More

How to resolve "Could not find or load main class package" in Java?

Maruthi Krishna
Updated on 14-Oct-2019 07:59:08

5K+ Views

Once you write a Java program you need to compile it using the javac command, this shows you the compile time errors occurred (if any).Once you resolve them and compile your program success fully, an executable file with the same name as your class name is generated in your current folder, with the .class extension.Then you need to execute it using the java command as −java class_nameWhile executing, when JVM does not find a .class file with the specified name then a run time error occurs saying “Could not found or load main class” error as −D:\sample>java Example Error: Could ... Read More

How to overwrite a line in a .txt file using Java?

Maruthi Krishna
Updated on 14-Oct-2019 07:55:25

16K+ Views

API’s usedThe replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.The java.util class (constructor) accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions. To read various datatypes from the source using the nextXXX() methods provided.The StringBuffer class is a mutable alternative to String, after instantiating this class you can add data to it using the append() method.ProcedureTo overwrite a particular line of a file −Read the contents ... Read More

Program to replace all the characters in of a file with '#' except a particular word in Java

Maruthi Krishna
Updated on 14-Oct-2019 07:51:55

477 Views

The split() method of the String class. splits the current string around matches of the given regular expression. The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.To replace all the characters in of a file with '#' except a particular word (one way) −Read the contents of a file to a String.Create ... Read More

Advertisements