Raja has Published 806 Answers

Can we define a parameterized constructor in an abstract class in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

Yes, we can define a parameterized constructor in an abstract class.Conditions for defining a parameterized constructor in an abstract classWe need to make sure that the class which is extending an abstract class have a constructor and it can call the superclass parameterized constructor.We can call the superclass parameterized constructor ... Read More

How can we call garbage collection (GC) explicitly in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

When there are no more references to an object, the object is finalized and when the Garbage Collection starts these finalized objects get collected this will done automatically by the JVM. We can call garbage collection directly but it doesn't guarantee that the GC will start executing immediately.We can call ... Read More

How to throw an exception from a static block in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

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 ... Read More

Why do we need a copy constructor and when should we use a copy constructor in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

A copy constructor is a parameterized constructor and it can be used when we want to copy the values of one object to another.Example:class Employee {    int id;    String name;    Employee(int id, String name)    {       this.id = id;       this.name ... Read More

Can a constructor throw an exception in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

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 ... Read More

Can we write any code after throw statement in Java?

raja

raja

Updated on 30-Jul-2019 22:30:26

No, we can not place any code after throw statement, it leads to compile time error Unreachable Statement.Throw keyword in JavaThe throw keyword is used to throw an exception manually.Whenever it is required to suspend the execution of the functionality based on the user-defined logical error condition, we will use this ... Read More

Advertisements