Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Articles - Page 309 of 745
6K+ Views
Yes, constructors are allowed to throw an exception in Java.A Constructor is a special type of a method that is used to initialize the object and it is used to create an object of a class using the new keyword, where an object is also known as an Instance of a class. Each object of a class will have its own state (Instance variables) and access to methods of its class.Throw an Exception from a ConstructorA checked exception can be used to indicate a legitimate problem when trying to create an instance, while an unchecked exception typically indicates a bug either in the ... Read More
6K+ Views
A static block is a set of statements, which will be executed by the JVM before the execution of the main() method. At the time of class loading if we want to perform any activity we have to define that activity inside a static block because this block executes at the time of class loading.Throw an exception from a Static BlockA static block can throw only a RunTimeException, or there should be a try and catch block to catch a checked exception.A static block occurs when a class is loaded by a class loader. The code can either come in the form ... Read More
2K+ Views
A class that is declared inside a class but outside a method is known as member inner class.We can instantiate a member Inner class in two waysInvoked within the classInvoked outside the classRules for Inner ClassThe outer class (the class containing the inner class) can instantiate as many numbers of inner class objects as it wishes, inside its code.If the inner class is public & the containing class as well, then code in some other unrelated class can as well create an instance of the inner class.No inner class objects are automatically instantiated with an outer class object.If the inner ... Read More
5K+ Views
In Java, both ClassNotFoundException and NoClassDefFoundError are issues that occur when the JVM or ClassLoader is not able to find the appropriate class at the time of loading (run-time). The ClassNotFoundException is a checked exception, and NoClassDefFoundError is an Error that comes under unchecked. There are different types of ClassLoaders, each responsible for loading classes from different sources such as directories, JAR files, or network locations. If a required class is missing due to an incorrect classpath or a missing JAR file, the ClassLoader might fail to load it. This situation leads to one of these two issues. The ClassNotFoundException in ... Read More
19K+ Views
Sometimes we may need to rethrow an exception in Java. If a catch block cannot handle the particular exception it has caught, we can rethrow the exception. The rethrow expression causes the originally thrown object to be rethrown.Because the exception has already been caught at the scope in which the rethrow expression occurs, it is rethrown out to the next enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks for the enclosing try block have an opportunity to catch the exception.Syntaxcatch(Exception e) { System.out.println("An exception ... Read More
538 Views
The Throwable class is a superclass of all errors and exceptions in Java. Objects that are instances of this class are thrown by the Java Virtual Machine or can be thrown by a throw statement. Similarly, this class or one of its subclasses can be the argument type in a catch clause.Instances of two subclasses Error and Exception are used to indicate that exceptional situations have occurred, these instances are created in the context of the exceptional situation to include relevant information.Commonly used exception methods of Throwable classpublic String getMessage(): returns the message string about the exception.public Throwable getCause(): returns the cause of the exception. ... Read More
48K+ Views
An interface can be used to define a contract for behavior that classes must implement, and it can also act as a contract between two systems to interact. An abstract class is mainly used to define default behavior for subclasses. All child classes inherited from the abstract class can override or extend its methods. If we compare an interface and an abstract class, both can contain abstract methods and cannot be instantiated directly. Also, both are used to achieve abstraction. We may often get confused about when to use an abstract class and when to use an interface in Java. ... Read More
15K+ Views
No, we can't create an object of an abstract class. But, we can create a reference variable of an abstract class. The reference variable is used to refer to the objects of derived classes (subclasses of abstract class). An abstract class hides the implementation and shows the function definition to the user. A Java abstract class can have instance methods that implement a default behavior and 0 or more abstract methods. A method which does not have body is known as abstract method. It contains only the method signature and an abstract keyword before it as shown below: public abstract ... Read More
34K+ Views
Constants in Java A constant is a variable whose value cannot be changed once it has been initialized. Java doesn't have built-in support for constants. To define a variable as a constant, we just need to add the keyword "final" in front of the variable declaration. It is not mandatory that we assign values to constants during declaration. Syntax of Java Constant final float pi = 3.14f; The above statement declares the float variable "pi" as a constant with a value of 3.14f. We cannot change the value of "pi" at any point in time in the program. ... Read More
17K+ Views
Declaring Multiple Classes in Java Program A single Java program may contain two or more classes, it is possible in two ways: Multiple non-nested classes Nested classes The Multiple non-nested Classes We can create as many classes as we want in a single Java program but writing many classes in a single file is not recommended as it makes code difficult to read rather we can create a single file for every class. When we compile a Java program with two or more classes (non-nested), the same number of .class ... Read More