Java Articles

Page 33 of 450

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ 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 try to use a blank final variable without initializing in the constructor, a compile time error will be generated.Examplepublic class Student {    public final String name;    public void display() {       System.out.println("Name of the Student: "+this.name);    }    public static void main(String args[]) {   ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 816 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 −Exampleclass Data {    static final int num;    Data(int i) {       num = i;    } } public class ConstantsExample {    public static void main(String args[]) {       System.out.println("value of the constant: "+Data.num);    } }Compile time errorConstantsExample.java:4: error: cannot assign a value to final ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 541 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 can still create a constant by declaring a variable static and final.Static − Once you declare a variable static they will be loaded into the memory at the compile time i.e. only one copy of them is available.Final − once you declare a variable final you cannot modify its value ...

Read More

Difference between next() and hasNext() in java collections?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

Java provides Iterator and ListIterator classes to retrieve the elements of the collection objects.The hasNext() methodThe hasNext() method of these interfaces returns true if the collection object has the next element else it returns false.Exampleimport java.util.ArrayList; import java.util.Iterator; public class hasNextExample{    public static void main(String[] args){       ArrayList list = new ArrayList();       //Instantiating an ArrayList object       list.add("JavaFX");       list.add("Java");       Iterator it = list.iterator();       System.out.println(it.hasNext());       it.next();       System.out.println(it.hasNext());       it.next();       System.out.println(it.hasNext());    } ...

Read More

How to remove the redundant elements from an ArrayList object in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 566 Views

The interface set does not allow duplicate elements. The add() method of this interface accepts elements and adds to the Set object, if the addition is successful it returns true if you try to add an existing element using this method, the addition operations fails to return false.Therefore, to remove redundant elements of an ArrayList object −Get/create the required ArrayList.Create an empty set object.Try to add all the elements of the ArrayList object to set objectives.Clear the contents of the ArrayList using the clear() method.Now, using the addAll() method add the contents of the set object to the ArrayList again.Exampleimport ...

Read More

How to insert an object in an ArrayList at a specific position in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

The add() method of the ArrayList class helps you to add elements to an array list. It has two variants −add(E e) − This method accepts an object/elements as a parameter and adds the given element at the end of the list.public void add(int index, E element) − This method accepts an element and an integer value representing the position at which we need to insert it and inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).Therefore ...

Read More

Difference between peek(), poll() and remove() method of Queue interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 16K+ Views

This represents a collection that is indented to hold data before processing. It is an arrangement of the type First-In-First-Out (FIFO). The first element put in the queue is the first element taken out from it.The peek() methodThe peek() method returns the object at the top of the current queue, without removing it. If the queue is empty this method returns null.Exampleimport java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class QueueExample {    public static void main(String args[]) {       Queue queue = new LinkedList();       queue.add("Java");       queue.add("JavaFX");       queue.add("OpenCV");     ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 398 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 should throw the same exception or its sub type.The method in the sub-class should not throw its super type.You can override it without throwing any exception.When you have three classes named Demo, SuperTest and, Super in (hierarchical) inheritance, if Demo and SuperTest have a method named sample().Exampleclass Demo {   ...

Read More

What is meant by re-throwing exceptions in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 5K+ Views

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).While re-throwing exceptions you can throw the same exception as it is without adjusting it as −try {    int result = (arr[a])/(arr[b]);    System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); } catch(ArithmeticException e) {    throw e; }Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing a ...

Read More

How to avoid ConcurrentModificationException while iterating a collection in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 429 Views

When you are working with collection objects, while one thread is iterating over a particular collection object, if you try to add or remove elements from it, a ConcurrentModificationException will be thrown.Not only that, If you are iterating a collection object, add or remove elements to it and try to iterate its contents again it is considered that you are trying to access the collection object using multiple threads and ConcurrentModificationException is thrown.Exampleimport java.util.ArrayList; import java.util.Iterator; public class OccurenceOfElements {    public static void main(String args[]) {       ArrayList list = new ArrayList();       //Instantiating ...

Read More
Showing 321–330 of 4,498 articles
« Prev 1 31 32 33 34 35 450 Next »
Advertisements