Java Articles

Page 141 of 450

What are the different types of classes in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 21-May-2025 3K+ Views

In Java, a class is a datatype which defines properties (variables) and behaviors (methods) of an object. Defining an object does not consume memory; only its object or instance does.Depending on the requirement, we will create various types of classes in Java. In this article, we are going to discuss them. Types of classes in Java The Java class is classified into different types based on its methods, as shown in the list given below: Concrete class Abstract class Final class POJO class Static class Inner Class Wrapper Class Singleton Class Concrete class Any normal class which does ...

Read More

What is the contract between equals() and hashCode() methods in Java?\\n

Shriansh Kumar
Shriansh Kumar
Updated on 21-May-2025 16K+ Views

Every Java object has two very important methods,  equals() and hashCode(), and these methods are designed to be overridden according to their specific general contract. Since the Object class is the parent class of every class, the default implementation of the equals() and hashCode() methods is already present in each class. However, we need to override these methods based on the requirement. Let's discuss the contract between equals() and hashCode() methods in Java. But before that, we need to understand these methods. The hashCode() Method The hashCode() method returns an integer value, which is referred to as the hash code value ...

Read More

Interface variables are static and final by default in Java, Why?

Shriansh Kumar
Shriansh Kumar
Updated on 21-May-2025 21K+ Views

In Java, interfaces are used to achieve abstraction and multiple inheritance. They can contain methods and variables, but there are specific rules about how those members should behave. For example, all variables declared in an interface are public, static, and final by default, even if you don't use these keywords while defining variables. To understand the reason behind it, we first need to understand what static and final mean in Java. What is a Static Variable in Java? The static variables are defined using the static keyword. These variables belong to the class rather than to any specific object, which ...

Read More

How to handle the ArrayStoreException (unchecked) in Java?\\n

Shriansh Kumar
Shriansh Kumar
Updated on 21-May-2025 386 Views

In Java, ArrayStoreException is a public class that extends the RuntimeException class of the java.lang package. It is thrown by the Java Virtual Machine when a runtime error occurs. Since it is an unchecked exception, it does not require an explicit declaration in a method or a constructor's throws clause. Let's understand why the ArrayStoreException is thrown and how we can avoid it. Reason for ArrayStoreException in Java As mentioned earlier, ArrayStoreException is an unchecked exception, and it can occur when we try to store an object of one type in an array of a different type. Usually, one would ...

Read More

How to set local date/time in a table using LocalDateTime class in Java?

Smita Kapse
Smita Kapse
Updated on 21-May-2025 4K+ Views

The java.time package of Java provides a class named LocalDateTime is used to get the current value of local date and time. Using this in addition to date and time values, you can also get other date and time fields, such as day-of-year, day-of-week, and week-of-year. Setting the Local time to a column To set the local date and time value to a column in a table. Obtain the LocalDateTime object: You can obtain the LocalDateTime object by invoking the static method now() as: LocalDateTime localDateTime = LocalDateTime.now(); Get the LocalDate and LocalTime objects from the above obtained LocalDateTime as: ...

Read More

How can we show a popup menu when the user right-clicks on a JComboBox in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 21-May-2025 602 Views

A JComboBox is a Swing component that has a built-in left-click menu. In this article, we will learn how to show a popup menu when the user right-clicks on a JComboBox in Java.  What is a JComboBox? A JComboBox is a subclass of the JComponent class that displays a drop-down list and gives users options that they can select only one item at a time. A JComboBox can be editable or read-only. The getSelectedItem() Method A getSelectedItem() method can be used to get the selected or entered item from a combo box. What is a Popup Menu? A popup menu is ...

Read More

How to set the shortcut key to a JCheckBox in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 21-May-2025 438 Views

In this article, we will learn to set the shortcut key to a JCheckBox in Java. To create a JCheckBox with a keyboard shortcut (Alt + C), we will use Java Swing. When we either click the checkbox with the mouse or press Alt+C, the checkbox toggles, and a message dialog is displayed. We can do this by using the setMnemonic('C') method to assign the shortcut key and an ActionListener to respond to the checkbox selection. What is a JCheckBox? A JCheckBox is a subclass of JToggleButton, and it can be a small box that is either checked or unchecked. When ...

Read More

How many ways to prevent method overriding in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 21-May-2025 4K+ Views

Method overriding works because of the run-time method binding feature in Java. So, if we force the Java compiler to do static binding for a method then we can prevent that method from being overridden in a derived class. Preventing Method Overriding in Java We can prevent method overriding in Java in 3 ways: By declaring a method as "final" in the base class By declaring a method as "static" in the base class By declaring a method as "private" in the base class Final Methods cannot be Overridden ...

Read More

What are the differences between StackOverflowError and OutOfMemoryError in Java?

Shriansh Kumar
Shriansh Kumar
Updated on 21-May-2025 1K+ Views

A Java program may contain an interface, a variable, a method, a class, and an object. When we execute the program, the operating system allocates some memory to Java Virtual Machine. Then, JVM divides allocated memory into two parts, which are the Heap and the Stack. The values of variables, methods, and classes are stored inside the Heap. And, the reference variables, method names, and classes are stored in the stack. When the Stack becomes full, the JVM throws a StackOverflowError, and when the heap becomes full, it throws an OutOfMemoryError. StackOverflowError in Java A stack is used for the execution of methods. It ...

Read More

What are the differences between the Heap memory and the String Constant Pool in Java?\\n

Shriansh Kumar
Shriansh Kumar
Updated on 21-May-2025 7K+ Views

The Heap Memory and String Constant Pool are two different memory locations in Java where objects are stored during the execution of programs. Both memory areas are managed by the Java Virtual Machine. In this article, we are going to discuss the difference between Heap Memory and String Constant Pool in Java. Heap Memory Heap memory is a runtime data area from which memory for all class instances and arrays is allocated. It is the main memory area used for dynamic memory allocation in Java. Example Creating strings with the new keyword always allocates new memory in the heap. In ...

Read More
Showing 1401–1410 of 4,496 articles
« Prev 1 139 140 141 142 143 450 Next »
Advertisements