Raja has Published 806 Answers

How to handle the exception using UncaughtExceptionHandler in Java?

raja
Published on 01-Jul-2019 11:15:19
The UncaughtExceptionHandler is an interface inside a Thread class. When the main thread is about to terminate due to an uncaught exception the java virtual machine will invoke the thread’s UncaughtExceptionHandler for a chance to perform some error handling like logging the exception to a file or uploading the log to the server before it ... Read More

How to change/increase the heap size of the Java Virtual Machine in Java?

raja
Published on 01-Jul-2019 09:01:10
A Java program can execute in the Java Virtual Machine (JVM) uses the heap memory to manage the data. If our Java program requires more memory, there is a possibility that the Java Virtual Machine(JVM) will begin to throw OutOfMemoryError instances when attempting to instantiate an object in Java.To Change/increase the JVM Heap ... Read More

Can we define a static constructor in Java?

raja
Published on 28-Jun-2019 14:54:58
No, we cannot define a static constructor in Java, If we are trying to define a constructor with the static keyword a compile-time error will occur.In general, static means class level. A constructor will be used to assign initial values for the instance variables. Both static and constructor are different ... Read More

When can we use intern() method of String class in Java?

raja
Published on 28-Jun-2019 13:18:43
The intern() method of String class can be used to deal with the string duplication problems in Java. Using intern() we can save a lot of memory consumed by duplicate string instances. A string is duplicate if it contains the same content as another string but it can be occupied different memory ... Read More

Can we change the order of public static void main() to static public void main() in Java?

raja
Published on 28-Jun-2019 11:14:02
Yes, we can change the order of public static void main() to static public void main() in Java, the compiler doesn't throw any compile-time or runtime error. In Java, we can declare access modifiers in any order, the method name comes last, the return type comes second to last and then ... Read More

Can we extend an enum in Java?

raja
Published on 28-Jun-2019 08:19:10
No, we cannot extend an enum in Java. Java enums can extend java.lang.Enum class implicitly, so enum types cannot extend another class.Syntaxpublic abstract class Enum> implements Comparable, Serializable {    // some statements }EnumAn Enum type is a special data type which is added in Java 1.5 version.An Enum is used to define ... Read More

What are the differences between a static block and a constructor in Java?

raja
Published on 28-Jun-2019 06:46:51
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 ... Read More

Can we define a method name same as class name in Java?

raja
Published on 27-Jun-2019 15:03:01
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.ExampleLive Demopublic class ... Read More

Can we declare a constructor as private in Java?

raja
Published on 27-Jun-2019 13:54:07
Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.Conditions for Private ConstructorA private constructor does not allow a class to be subclassed.A private ... Read More

What will happen when we try to override final method of the superclass in Java?

raja
Published on 27-Jun-2019 12:31:17
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 super class we will get an error in Java.Rules for implementing Method OverridingThe method declaration should be the same as that of the method that is to be ... Read More
Advertisements