Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 8 of 50

What is the difference between getter/setter methods and constructor in Java?

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

ConstructorsA constructor in Java is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have the same name as their class and, have no return type.If you do not provide a constructor the compiler defines one on your behalf, which initializes the instance variables with default values.You can also accept parameters through constructors and initialize the instance variables of a class using the given values, these are known as parameterized constructors.ExampleThe following Java program has a class named student ...

Read More

Why subclass doesn't inherit the private instance variables of superclass in Java?

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

When you declare the instance variables of a class private, you cannot access them in another class if you try to do so a compile-time error will be generated.But, if you inherit a class that has private fields, including all other members of the class the private variables are also inherited and available for the subclass.But, you cannot access them directly, if you do so a compile-time error will be generated.Exampleclass Person{    private String name;    public Person(String name){       this.name = name;    }    public void displayPerson() {       System.out.println("Data of the Person ...

Read More

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 775 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 522 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

How to make elements of array immutable in Java?

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

No, you cannot make the elements of an array immutable.But the unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.And the asList() method of the ArrayList class accepts an array and returns a List object.Therefore, to convert an array immutable −Obtain the desired array.Convert it into a list object using the asList() method.Pass the obtained list as a parameter to the unmodifiableList() method.Exampleimport java.util.Arrays; import java.util.Collections; import java.util.List; public class UnmodifiableExample { ...

Read More

Can we initialize static variables in a default constructor in Java?

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

Class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the class, there is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.If you declare a static variable in a class, if you haven’t initialized it, just like with instance variables compiler initializes these with default values ...

Read More

How to find the Strings within a text file in Java?

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

Following Java program accepts a String value from the user, verifies whether a file contains the given String and prints the number of occurrences of the word too.Exampleimport java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class FindingWordFromFile {    public static void main(String args[]) throws FileNotFoundException {       //Reading the word to be found from the user       Scanner sc1 = new Scanner(System.in);       System.out.println("Enter the word to be found");       String word = sc1.next();       boolean flag = false;       int count = 0;       System.out.println("Contents ...

Read More

Exception Hierarchy in case of multiple catch blocks.

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

An exception is an issue (run time error) that occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Multiple exceptions in a codeWhenever we have a code that may generate more than one exception and if you need to handle them specifically you can use multiple catch blocks on a single try.try{    //code } catch(Exception1 ex1) {    // } catch(Exception2 ex2) {    // }Exampleimport java.util.Arrays; import java.util.Scanner; public class MultipleCatchBlocks {    public static void main(String [] args) ...

Read More

How to read/write data from/to .properties file in Java?

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

The .properties is an extension in java which is used to store configurable application. It is represented by the Properties class in Java, you can store a properties file and read from it using the methods of this class. This class inherits the HashTable class.Creating a .properties file −To create a properties file −Instantiate the Properties class.Populate the created Properties object using the put() method.Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.ExampleThe Following Java program creates a properties file in the path D:/ExampleDirectory/import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class CreatingPropertiesFile {   ...

Read More
Showing 71–80 of 500 articles
« Prev 1 6 7 8 9 10 50 Next »
Advertisements