Maruthi Krishna has Published 951 Articles

Can a final keyword alone be used to define a constant in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:41:59

297 Views

A constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program.Unlike other languages java does not support constants directly. But, you ... Read More

Why Java wouldn't allow initialization of static final variable in a constructor?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:37:04

560 Views

If you declare a variable static and final you need to initialize it either at declaration or, in the static block. If you try to initialize it in the constructor, the compiler assumes that you are trying to reassign value to it and generates a compile time error −Example Live Democlass ... Read More

Why should a blank final variable be explicitly initialized in all Java constructors?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:35:01

1K+ Views

A final variable which is left without initialization is known as blank final variable.Generally, we initialize instance variables in the constructor. If we miss out they will be initialized by the constructors by default values. But, the final blank variables will not be initialized with default values. So if you ... Read More

Why a constructor cannot be final in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:32:11

3K+ Views

Whenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass's final method from the subclass.i.e. The purpose of making a method final is to prevent modification of a method from outside (child class).In inheritance whenever you extend a class. The child class ... Read More

How to access a derived class member variable by an interface object in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:30:06

612 Views

When you try to hold the super class’s reference variable with a sub class object, using this object you can access the members of the super class only, if you try to access the members of the derived class using this reference you will get a compile time error.Example Live Demointerface ... Read More

Is parent child hierarchy important on throws while overriding in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 14-Oct-2019 10:46:54

198 Views

When you try to handle an exception (checked) thrown by a particular method, you need to catch it using the Exception class or super class of the Exception occurred.In the same way while overriding the method of a super class, if it throws an exception −The method in the sub-class ... Read More

Can we override only one method while implementing Java interface?

Maruthi Krishna

Maruthi Krishna

Updated on 14-Oct-2019 10:12:28

906 Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.To create an object of this type you need to implement this interface, provide ... Read More

Is it possible to override toString method in an array using Java?

Maruthi Krishna

Maruthi Krishna

Updated on 14-Oct-2019 09:14:21

1K+ Views

You can override the toString() method of the Object class but, if you are creating an object array of a particular class and want to print the contents of this array by overriding the toString() method instead of the, you cannot do that there is no solution for that in ... Read More

In how many ways you can retrieve the elements of a collection in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 14-Oct-2019 09:03:48

2K+ Views

You can retrieve the contents of a collection object in three ways −Using for each loopThe foreach loop or enhanced for loop, which enables you to traverse the complete collection object sequentially.Example Live Demoimport java.util.ArrayList; public class RetrievingData {    public static void main(String[] args) {       ArrayList ... Read More

How many ways are there to convert an Array to ArrayList in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 14-Oct-2019 08:59:09

182 Views

By adding each element of the arrayThe add() method of the ArrayList class accepts an element and adds it to the current array list. To convert an array to array list using this method −Get the string array.Create an empty ArrayList object.Add each element of the array to the ArrayList.Print ... Read More

Advertisements