Use of Final Keyword to Define a Constant in Java

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

494 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

Why Java Doesn't Allow Initialization of Static Final Variable in a Constructor

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

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

Blank Final Variable Initialization in Java Constructors

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

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.Example Live Demopublic 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 a Constructor Cannot Be Final in Java

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

4K+ 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 inherits all the members of the superclass except the constructors.In other words, constructors cannot be inherited in Java therefore you cannot override constructors.So, writing final before constructors makes no sense. Therefore, java does not allow final keyword before a constructor.If you try, make a constructor final a compile time error ... Read More

Access Derived Class Member Variable by Interface Object in Java

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

774 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 Sample {    void demoMethod1(); } public class InterfaceExample implements Sample {    public void display() {       System.out.println("This ia a method of the sub class");    }    public void demoMethod1() {       System.out.println("This is demo method-1");    }    public static void main(String args[]) ... Read More

Parent-Child Hierarchy and Throws in Java Overriding

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

356 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().Example Live Democlass Demo { ... Read More

Convert LinkedList to Array in Java

Maruthi Krishna
Updated on 14-Oct-2019 08:51:15

6K+ Views

The toArray() method of the LinkedList class converts the current Linked List object into an array of object type and returns it. This array contains all of the elements in this list in proper sequence (from first to last element). This acts as bridge between array-based and collection-based APIs.Therefore, to convert a LinkedList to an array −Instantiate the LinkedList class.Populate it using the add() method.Invoke the toArray() method on the above created linked list and retrieve the object array.Convert each and every element of the object array to string.Example Live Demoimport java.util.Arrays; import java.util.LinkedList; public class LinkedListToArray {    public static ... Read More

Resolve Could Not Find or Load Main Class Package in Java

Maruthi Krishna
Updated on 14-Oct-2019 07:59:08

6K+ Views

Once you write a Java program you need to compile it using the javac command, this shows you the compile time errors occurred (if any).Once you resolve them and compile your program success fully, an executable file with the same name as your class name is generated in your current folder, with the .class extension.Then you need to execute it using the java command as −java class_nameWhile executing, when JVM does not find a .class file with the specified name then a run time error occurs saying “Could not found or load main class” error as −D:\sample>java Example Error: Could ... Read More

Replace All Characters in a File with Hash Except a Particular Word in Java

Maruthi Krishna
Updated on 14-Oct-2019 07:51:55

641 Views

The split() method of the String class. splits the current string around matches of the given regular expression. The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.To replace all the characters in of a file with '#' except a particular word (one way) −Read the contents of a file to a String.Create ... Read More

Parse Date from String in the Format DD-MM-YYYY to DD-MM-YYYY in Java

Maruthi Krishna
Updated on 14-Oct-2019 07:46:50

15K+ Views

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).One of the constructors of this class accepts a String value representing the desired date format and constructors SimpleDateFormat object.The format() method of this class accepts a java.util.Date object and returns a date/time string in the format represented by the current object.Therefore, to parse a date String to another date format −Get the input date string.Convert it into java.util.Date object.Instantiate the SimpleDateFormat class by passing the desired (new) format as string to its constructor.Invoke the format() method by passing the above ... Read More

Advertisements