Java Articles

Page 126 of 450

What is the order of execution of non-static blocks with respect to a constructor in Java?

Alshifa Hasnain
Alshifa Hasnain
Updated on 26-Aug-2025 3K+ Views

In Java, non-static blocks run before the constructor whenever an object is created. If multiple non-static blocks exist, they execute in the order they appear in the class, followed by the constructor. In this article, we will understand this order of execution with the help of examples. What Are Non-Static Blocks in Java? The Non-static blocks are class-level blocks which don't contain a prototype. A non-static block must perform any logic whenever an object is instantiated, regardless of the constructor. The Non-static blocks are automatically called by the JVM for every object creation in the Java stack area. We can ...

Read More

Can we define constructor inside an interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 26-Aug-2025 8K+ Views

Interfaces in Java are used for defining a contract that classes can implement. They can contain method signatures, default methods, static methods, and constants. we must implement the methods defined in the interface in the implementing class. Can we define a constructor inside an interface in Java? No, constructors cannot be defined inside an interface in Java. Constructors are special methods that are used to initialize objects of a class, and since interfaces cannot be instantiated, they do not have constructors. Why can constructors not be defined in an interface? There are several reasons why constructors cannot be defined ...

Read More

Java program to print downward triangle star pattern

Manisha Chand
Manisha Chand
Updated on 22-Aug-2025 1K+ Views

In this article, we will understand how to print a downward triangle star pattern using Java. In this pattern, the face of the triangle will be in a downward direction, and the base will be in an upward direction. We will learn through two examples: one where the user inputs the number of rows, and another where the number of rows is predefined in the program. Problem Statement Write a Java program to print a downward triangle star pattern. Below is a demonstration of the same. Input: Enter the number of rows : 8 Output: The downward triangle star pattern ...

Read More

Java Program to Interchange the Diagonals

Manisha Chand
Manisha Chand
Updated on 22-Aug-2025 430 Views

In this article, we will understand how to interchange the diagonal elements of a given matrix in Java. A matrix is a two-dimensional array made up of rows and columns. A matrix with m rows and n columns is called an m × n matrix. Each element in the matrix is represented by a[i][j], which means that the particular element a is present in the i-th row and j-th column. Problem Statement Write a Java program to interchange the elements of the primary and secondary diagonals of a square matrix. Example Scenario ...

Read More

Constructor overloading in enumerations in Java.

Maruthi Krishna
Maruthi Krishna
Updated on 21-Aug-2025 1K+ Views

Overloading is one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters. It allows one method to perform different tasks based on the parameters passed to it. Whenever you call this method, the method body will be bound with the method call based on the parameters we pass to it. Constructor Overloading in Java Constructors are special methods that are called when an object is created. Constructor overloading in Java allows a class to have multiple constructors with different parameters. This is useful when you want to create objects in ...

Read More

What is the return type of a Constructor in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Aug-2025 7K+ Views

A Constructor in Java is similar to a method, and it is invoked at the time of creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class. The main purpose of a constructor is to initialize the instance variables of a class. Return Type of Constructor In Java, constructors do not have a return type, not even void. They are used to initialize the object when it is created. A constructor doesn’t have any return type. ...

Read More

How to implement a constructor reference with one or more arguments in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 20-Aug-2025 10K+ Views

In this article, we are going to learn about implementing a constructor reference with an argument or multiple arguments in Java. Constructor references in Java provide us with a concise way of creating new objects via method references. These are going to enable us to reference a constructor without running the constructor itself so the code looks cleaner and it will be more readable. What is a Constructor Reference? A method reference may also be used with Java 8 constructors. A constructor reference may be defined by employing the class name and a new keyword. The constructor reference may be ...

Read More

How to create a constructor reference for an array in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 20-Aug-2025 4K+ Views

In Java, a constructor refers to a special method within a class that is always executed automatically while creating an instance of the class using the new keyword. Its main purpose is initializing the object by providing its fields with initial values so that it is ready for use. A constructor shares the name of the class itself, has no return type, and can be overloaded so that there will be different ways of initializing the objects. If you don’t provide one, Java inserts a default no-argument constructor automatically. What is a Constructor Reference in Java? Constructor reference is like ...

Read More

Java program to split the array and add the first part to the end

Alshifa Hasnain
Alshifa Hasnain
Updated on 12-Aug-2025 413 Views

Our task is to write a Java program to split the array and add the first part to the end. Here, the value of k=2, which means we need to keep 2 elements at the end of the array. Input: 67, 45, 78, 90, 12, 102, 34 Output: 78, 90, 12, 102, 34, 67, 45 Split the Java Array, Add the First Part to the End The following are the approaches to split the array and add the first part to the end: By shifting elements Using the reversal method ...

Read More

Can an anonymous class have constructors in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 08-Aug-2025 2K+ Views

Anonymous Classes in Java are classes that do not have a name. They are typically used to extend a class or implement an interface without the need for a separate named class. The following are the common uses of anonymous classes:Implementing event listeners.Creating Runnable objects for threads.Providing custom implementations for abstract methods of an abstract class. Constructors in an Anonymous class Anonymous classes cannot have a constructor, but the compiler provides a default constructor for them. This means that when you create an instance of an anonymous class, it will call the constructor of the superclass or the interface it ...

Read More
Showing 1251–1260 of 4,498 articles
« Prev 1 124 125 126 127 128 450 Next »
Advertisements