Found 7442 Articles for Java

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

Vivek Verma
Updated on 27-May-2025 15:07:58

2K+ Views

In Java, overriding is a technique used to provide a specific implementation of a method that is already defined in a superclass (i.e., parent class), allowing a subclass (i.e., child class) to modify or extend the behavior of that method. Purpose of Overriding the finalize() Method 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 this method, then we need to call the finalize() method explicitly. The finalize() method can be ... Read More

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

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

1K+ 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 JButton by overriding the mouseEntered() method of the MouseListener interface.Syntaxvoid mouseEntered(MouseEvent e)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class RollOverButtonTest extends JFrame {    private JButton button;    public RollOverButtonTest() {       setTitle("RollOverButton Test");       button = new JButton("Rollover Button");       button.addMouseListener(new MouseAdapter() {     ... Read More

How can we avoid a deadlock in Java?

Vivek Verma
Updated on 14-May-2025 14:07:06

5K+ Views

This article explains several strategies to avoid deadlock in Java, including a brief introduction to deadlock, strategies, and respective examples. Deadlock in Java In case of multi-threading, a deadlock is a programming situation where two or more threads are holding resources needed by other threads and are blocked forever, waiting for each other (to release the required resource). A deadlock condition will occur with at least two threads and two or more resources.How To Avoid Deadlock in Java? Following is a list of strategies to avoid the deadlock in Java: ... Read More

Importance of yield() method in Java?

raja
Updated on 24-Nov-2023 10:37:40

13K+ Views

A yield() method is a static method of Thread class and it can stop the currently executing thread and will give a chance to other waiting threads of the same priority. If in case there are no waiting threads or if all the waiting threads have low priority then the same thread will continue its execution. The advantage of yield() method is to get a chance to execute other waiting threads so if our current thread takes more time to execute and allocate processor to other threads. Syntax public static void yield() Example class MyThread extends Thread { public void run() { ... Read More

Can we define multiple methods in a class with the same name in Java?

Vivek Verma
Updated on 14-May-2025 13:11:32

8K+ Views

Yes, we can define multiple methods in a class with the same name. But if we have two methods with the same name, the compiler should be able to differentiate between the two methods. Therefore, in Java, "we can define multiple methods" with the same name in a single class, as long as each method has a different set of parameters. When we invoke a method, the compiler executes the respective body (code) based on the arguments passed.This concept is known as method overloading. Method Overloading in Java In Java, method overloading is a type of compile-time polymorphism. Polymorphism is one ... Read More

How to select different cells of a JTable programmatically in Java?

raja
Updated on 12-Feb-2020 06:17:43

1K+ Views

A JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns. When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface.In general, a user can select the rows and columns manually in a JTable, we can also select different cells of a JTable programmatically using setRowSelectionInterval() and setColumnSelectionInterval() methods of JTable class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTableCellSelectionTest extends JFrame {    private JTable table;    public JTableCellSelectionTest() {       setTitle("JTableCellSelection Test");       Object[][] data = ... Read More

How can we sort a string without using predefined methods in Java?

Vivek Verma
Updated on 14-May-2025 12:39:37

7K+ Views

The java.lang.String class represents an immutable sequence of characters and cannot be changed once created. We need to instantiate this class or assign values directly to its literal to create a string in Java. The String class does not provide any built-in method to sort the contents of a string. To sort a String, we need to convert it into a character array using the toCharArray() method and sort the array. To sort a character array, we can either use the Arrays.sort() method or use sorting algorithms.  Since the given task is to sort a string without using any predefined methods, we ... Read More

What is the use of Object Cloning in Java?

Vivek Verma
Updated on 27-May-2025 15:54:32

2K+ Views

The article will explain the use of object cloning in Java and provide an appropriate example to clarify the problem. What is the Object Cloning in Java? In Java, object cloning is a way to create an exact copy of an object. We can use the clone() method provided by the Object class to create a clone of an object. When an object is cloned, a new instance of the same class is created, and the fields of the original object are copied to the new object. The Cloneable interface must be implemented by a class whose object is ... Read More

When to call the Thread.run() instead of Thread.start() in Java?

Vivek Verma
Updated on 14-May-2025 12:15:29

2K+ Views

This article will briefly discuss the run() and start() methods, and also explain when to call run() instead of the start() method. Calling run() Method Instead of start() Usually, to execute a thread, we will call the start() method, and the start method calls run() implicitly, and the contents of the run() method will be executed as a separate thread. We can also call the run() method explicitly instead of the start() method. If we do so, we will be executing the contents of the run method in the current thread but not in a separate one. In this case, the ... Read More

How to detect the value change of a JSlider in Java?

raja
Updated on 12-Feb-2020 05:32:30

1K+ Views

A JSlider is a subclass of JComponent class and it is similar to scroll bar which allows the user to select a numeric value from a specified range of integer values. A JSlider has a knob which can slide on a range of values and can be used to select a particular value. and it can generate a ChangeListener interface.We can detect the value changed when the slider is moved horizontally using the Graphics2D class and override paint() method.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class ValueChangeJSliderTest extends JFrame {    private JSlider slider;    public ValueChangeJSliderTest() { ... Read More

Advertisements