No, you cannot create an object of a final class in Java.
Here's why:
- Final Classes are Immutable: When a class is declared as
final
, it becomes immutable. This means you cannot extend or inherit from it. - No Subclasses: Since you cannot create subclasses of a final class, you cannot create objects of a final class using the
new
keyword.
Example:
final class MyFinalClass {
// Class definition
}
// This will result in a compile-time error:
MyFinalClass obj = new MyFinalClass();
Practical Insight:
Final classes are often used to prevent accidental modification or extension of a class. This ensures the integrity and consistency of the class's behavior.
Example Use Cases:
- String Class: The
String
class in Java is final to prevent modification of its internal representation. - Math Class: The
Math
class is final to ensure its mathematical functions remain consistent and reliable.