Raja has Published 760 Articles

Can a constructor be synchronized in Java?

raja

raja

Updated on 02-Jul-2020 13:00:23

2K+ Views

No, a constructor cannot be synchronized in Java. The JVM ensures that only one thread can invoke a constructor call at a given point in time. That is why no need to declare a constructor as synchronized and it is illegal in Java. However, we can use synchronized blocks inside a constructor.If ... Read More

Importance of SerialVersionUID keyword in Java?

raja

raja

Updated on 02-Jul-2020 09:02:37

5K+ Views

SerialVersionUIDThe SerialVersionUID must be declared as a private static final long variable in Java. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the JVM to identify the state of an object when it reads ... Read More

How many ways to call garbage collector (GC) in Java?

raja

raja

Updated on 02-Jul-2020 08:57:04

1K+ Views

The garbage collection in Java is carried by a daemon thread called Garbage Collector(GC). Instead of waiting until JVM to run a garbage collector we can request JVM to run the garbage collector. There is no guarantee whether the JVM will accept our request or not.In Java, we can call ... Read More

How can we call the invokeLater() method in Java?

raja

raja

Updated on 02-Jul-2020 08:12:57

3K+ Views

An invokeLater() method is a static method of the SwingUtilities class and it can be used to perform a task asynchronously in the AWT Event dispatcher thread. The SwingUtilities.invokeLater() method works like SwingUtilities.invokeAndWait() except that it puts the request on the event queue and returns immediately. An invokeLater() method does not wait for the block ... Read More

What is the purpose of overriding a finalize() method in Java?

raja

raja

Updated on 02-Jul-2020 07:25:16

1K+ Views

The finalize() method is a pre-defined method in the Object class and it is protected. The purpose of a finalize() method can be overridden for an object to include the cleanup code or to dispose of the system resources that can be done before the object is garbage collected. If we are overriding ... Read More

How to implement a rollover effect for a JButton in Java?

raja

raja

Updated on 02-Jul-2020 07:04:18

966 Views

A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons to a GUI application. A JButon can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. We can implement the rollover effect when the mouse moves over a ... Read More

What is the use of Object Cloning in Java?

raja

raja

Updated on 02-Jul-2020 05:31:09

2K+ Views

The object cloning is a way to create an exact copy of an object. For this purpose, the clone() method of an object class is used to clone an object. The Cloneable interface must be implemented by a class whose object clone to create. If we do not implement Cloneable interface, clone() method ... Read More

How to read the data from a CSV file in Java?

raja

raja

Updated on 01-Jul-2020 12:10:16

21K+ Views

A CSV stands for Comma Separated Values. In a CSV file, each line contains words that are separated with a comma(, ) and it is stored with a .csv extension.We can read a CSV file line by line using the readLine() method of BufferedReader class. Split each line on comma character to get the ... Read More

What is the importance of a FocusListener interface in Java?

raja

raja

Updated on 01-Jul-2020 10:10:12

513 Views

FocusListenerThe focus events are generated whenever a component gains or loses the keyboard focus.The Objects representing focus events are created from FocusEvent Class.The corresponding listener interface for FocusEvent class is a FocusListener interface. Each listener for FocusEvent can implement the FocusListener interface.The FocusListener interface contains two methods focusGained(): Called by the AWT just after the listened-to ... Read More

How to print a formatted text using printf() method in Java?

raja

raja

Updated on 01-Jul-2020 08:12:21

436 Views

The printf() method allows us to format output to a java.io.PrintStream or java.io.PrintWriter. These classes also contain a method called format() which can produce the same results, so whatever we read here for the printf() method can also be applied to the format() method.SyntaxSystem.out.printf(“format-string” [, arg1, arg2, … ]);Example1import java.io.PrintStream; ... Read More

Advertisements