Found 7442 Articles for Java

Can a method local inner class access the local final variables in Java?

raja
Updated on 29-Jun-2020 11:12:53

832 Views

Yes, we can access the local final variables using the method local inner class because the final variables are stored on the heap and live as long as the method local inner class object may live.Method Local Inner ClassA local inner class instance can be delivered as an argument and retrieved from the methods and it is available inside a valid scope.The only limitation in method local inner class is that a local parameter can be executed only when it is defined as final.The method executing the local parameters can be called after the execution of the method, within which the local inner ... Read More

How to handle the ArrayStoreException (unchecked) in Java?

Shriansh Kumar
Updated on 21-May-2025 14:59:40

327 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 handle the NumberFormatException (unchecked) in Java?

Shriansh Kumar
Updated on 27-May-2025 10:50:11

2K+ Views

The NumberFormatException is a class that represents an unchecked exception thrown by parseXXX() methods when they are unable to format (convert) a string into a number. Here, the parseXXX() is a group of Java built-in methods that are used to convert string representations of numbers into their corresponding primitive data types. Causes for NumberFormatException in Java The NumberFormatException extends IllegalArgumentException class of java.lang package. It can be thrown by many methods/constructors of the classes. Following are some of them: int parseInt(String s): It throws NumberFormatException if ... Read More

How to handle the ArithmeticException (unchecked) in Java?

Shriansh Kumar
Updated on 27-May-2025 11:31:15

7K+ Views

Java ArithmeticException The ArithmeticException of java.lang package is an unchecked exception in Java. It is raised when an attempt is made to use a wrong mathematical expression in the Java program. An example of ArithmeticExeption is division by 0. If you divide two numbers and the number in the denominator is zero. The Java Virtual Machine will throw ArithmeticException. Example: Throwing ArithmeticException In the following Java program, we try a mathematical operation where we divide a number by 0. public class ArithmeticExceptionTest { public static void main(String[] args) { int a ... Read More

What are the differences between protected and default access specifiers in Java?

Shriansh Kumar
Updated on 16-Apr-2025 18:59:58

11K+ Views

The protected and default access modifiers determine how a member of a class or method can be accessed. The modifiers are attached to the members at the time of declaration. Since Java follows the object-oriented programming paradigm, these access modifiers are used in encapsulation, polymorphism, and inheritance to control the behavior of class members. We will try to understand the difference between protected and default access modifiers in Java through this article. Protected Access Modifier The protected access modifier is mostly used in the case of inheritance to control the access of parent class members and corresponding child class members. ... Read More

What is an OutOfMemoryError and steps to find the root cause of OOM in Java?

raja
Updated on 24-Feb-2020 11:27:10

2K+ Views

The OutOfMemoryError is thrown by JVM, when JVM does not have enough available memory, to allocate. OutOfMemoryError falls into the Error category in Exception class hierarchy.To generate OutOfMemoryErrorWe will allocate a large chunk of memory, which will exhaust heap memory storage.We will keep on allocating the memory and point will reach, when JVM will not have enough memory to allocate, then OutOfMemoryError will be thrown.Once we will catch the OutOfMemory error, we can log the error.ExampleLive Demopublic class OutOfMemoryErrorDemo {    public static void main(String[] args) throws Exception {       int dummyArraySize = 15;       System.out.println("Max JVM memory: " + Runtime.getRuntime().maxMemory()); ... Read More

Can we write any code after throw statement in Java?

Shriansh Kumar
Updated on 16-May-2025 18:29:43

2K+ Views

No, we can not place any code after throw statement, it leads to compile time error. The compiler will show this error as unreachable statement. In Java, the throw statement immediately terminates the current flow of execution, therefore, the code immediate to a throw statement will not be executed. The control is then transferred to the next catch block or the caller method. The throw Keyword in Java The throw keyword is used to throw an exception manually. Whenever it is required to stop the execution of the functionality based on the user-defined logical error condition, we will use this ... Read More

How many types of anonymous inner classes are defined in Java?

raja
Updated on 11-Feb-2020 09:26:32

1K+ Views

An anonymous inner class is an inner class which is declared without any class name at all. In other words, a nameless inner class is called an anonymous inner class. Since it does not have a name, it cannot have a constructor because we know that a constructor name is the same as the class name.We can define an anonymous inner class and create its object using the new operator at the same time in one step.Syntaxnew(argument-list){    // Anonymous class body }Types of Anonymous Inner Class in JavaAnonymous inner class that extends a classAnonymous inner class that implements an interfaceAnonymous inner ... Read More

What are the differences between import and static import statements in Java?

Shriansh Kumar
Updated on 20-May-2025 18:22:18

5K+ Views

The import statement is used to bring certain classes and interfaces from other packages into our Java program, so we can access them without using their fully qualified names. We can use the short name instead. Java also supports static import statements, which were introduced in Java 5. It helps in accessing static members such as methods and constants. In this article, we are going to learn the difference between import and static import statements in Java. The import Statement in Java To access a class or method from another package, we need to either use the fully qualified ... Read More

What are the differences between compareTo() and compare() methods in Java?

Shriansh Kumar
Updated on 20-May-2025 19:28:58

9K+ Views

The compareTo() and compare() methods are both used to compare two objects. They return an int value after comparison, which tells if both objects are equal, if not, which is lesser, and which is greater. The primary difference between these two methods is that the compareTo() method is used when the Comparable interface is implemented, whereas the compare() method is used when the Comparator interface is implemented. Let's understand the use of compareTo() and compare() methods in Java, and then we will discuss how these two methods are different from each other. The compareTo() Method in Java The compareTo() method compares the ... Read More

Advertisements