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 306 of 745
2K+ Views
Static blockThe static blocks are executed at the time of class loading.The static blocks are executed before running the main () method.The static blocks don't have any name in its prototype.If we want any logic that needs to be executed at the time of class loading that logic needs to placed inside the static block so that it will be executed at the time of class loading.Syntaxstatic { //some statements }ExampleLive Demopublic class StaticBlockTest { static { System.out.println("Static Block!"); } public static void main(String args[]) { System.out.println("Welcome to Tutorials Point!"); } }OutputStatic Block! ... Read More
5K+ Views
A method is a collection of statements that are grouped together to perform an operation. In Java, you can define a method having the same name as the class but it is not recommended as per the coding standard. Can We define a Method Having Same as Class? Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur. But this is not recommended as per coding standards in Java. Normally the constructor name and class name always the same in Java. Example of a ... Read More
11K+ Views
Java constructor is a special method used to initialize objects. It has the same name as the class and is automatically called when an object is created. You can also declare a constructor as private to restrict object creation from outside the class. What is a Private Constructor? A private constructor is a constructor that is declared using the private access modifier. It restricts object creation from outside the class i.e., the class having a private constructor cannot be subclassed. Private constructors are used in design patterns like Singleton, or to prevent subclassing or instantiation. Purpose of a Private ... Read More
2K+ Views
Any method that is declared as final in the superclass cannot be overridden by a subclass. If we try to override the final method of the super class, we will get a compile-time error. Rules for Implementing Method Overriding The method declaration should be the same as that of the method that is to be overridden. The class (subclass) should extend another class (superclass), prior to even try overriding. The Sub Class can never override final methods of the Super Class. Final method A method is declared final when ... Read More
2K+ Views
In this article, we will be learning about ClassCastException in Java. But before we jump into this, we'll understand what an exception is in Java. After that, we'll learn why and when ClassCastException occurs, look at real-world code examples that throw this exception, and finally learn how to avoid or resolve it. What is an Exception in Java? An exception in Java is an event that disrupts the normal flow of the program. Java provides a way to handle these errors gracefully using try-catch blocks. There are 2 types of exceptions: Checked Exceptions – ... Read More
1K+ Views
In Java, it is possible to define a class inside another class, such classes are called Nested classes. We can use the access modifiers like private, public, protected or default for inner classes and default or public access modifiers for outer class.There are two types of nested classes are defined in Java.Static Nested ClassNon-Static Nested ClassStatic Nested ClassWe Can define an inner class as static, so such type of classes is called a static nested class.The nested class is defined with the static keyword, so this type of nested classes doesn’t share any relationship with the instance of an outer class.A static ... Read More
4K+ Views
In Java, both the length property and the length() method are used to determine the size of data, or we can say they help us to find the number of elements an object contains. However, they are used with different Java objects. The length is an instance variable used for arrays, whereas length() is a method used with String objects. In this article, we are going to discuss the difference between length and length() in Java. The length Property An array is an object that holds a fixed number of values of the same type. Its length variable is used to find the number ... Read More
10K+ Views
Yes, we can declare a try-catch block within another try-catch block in Java, this is called nested try-catch block. The try-catch block is used to handle runtime errors that occur during the execution of a program, and the process of handling these errors is called exception handling. The runtime errors or exceptions must be handled in order to maintain the normal flow ofaJava program. Try-Catch Block in Java In a try-catch block, the code that might throw an exception should be placed or written within a try block. The catch block is used to handle the exception that is thrown from ... Read More
8K+ Views
No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier). If it does not have a modifier, it is supposed to have a default access.Syntax// A top level class public class TopLevelClassTest { // Class body }If a top-level class is declared as private the compiler will complain that the modifier private is not allowed here. This means that a top-level class cannot be a private, the same can be applied to protected access specifier also.Protected means that the member can be accessed by any class in the same ... Read More
842 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