Found 2616 Articles for Java

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

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

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

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

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

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

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

613 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

How can we convert a JSONArray to String Array in Java?

raja
Updated on 06-Jul-2020 12:42:31

3K+ Views

The JSON is one of the widely used data-interchange formats. It is a lightweight and language independent. A JSONArray can parse text from a String to produce a vector-like object and supports java.util.List interface.We can convert a JSONArray to String Array in the below exampleExampleimport org.json.*; import java.util.*; public class JsonArraytoStringArrayTest {    public static void main(String[] args) {       JSONArray jsonArray = new JSONArray();       jsonArray.put("INDIA ");       jsonArray.put("AUSTRALIA ");       jsonArray.put("SOUTH AFRICA ");       jsonArray.put("ENGLAND ");       jsonArray.put("NEWZEALAND ");       List list = new ArrayList();       for(int i=0; ... Read More

How to convert a List to JSON array using the Jackson library in Java?

raja
Updated on 06-Jul-2020 12:43:38

10K+ Views

The ObjectMapper class is the most important class in the Jackson API that provides readValue() and writeValue() methods to transform JSON to Java Object and Java Object to JSON. We can convert a List to JSON array using the writeValueAsString() method of ObjectMapper class and this method can be used to serialize any Java value as a String.Syntaxpublic String writeValueAsString(Object value) throws JsonProcessingExceptionExampleimport java.util.*; import com.fasterxml.jackson.databind.*; public class ListToJSONArrayTest {    public static void main(String args[]) {       List list = new ArrayList();       list.add("JAVA");       list.add("PYTHON");       list.add("SCALA");       list.add(".NET");   ... Read More

How to define alternate names to a field using Jackson in Java?

raja
Updated on 17-Feb-2020 07:20:53

4K+ Views

The @JsonAlias annotation can define one or more alternate names for the attributes accepted during the deserialization, setting the JSON data to a Java object. But when serializing, i.e. getting JSON from a Java object, only the actual logical property name is used instead of the alias.Syntax@Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonAliasExampleimport com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class ObjectToJsonTest {    public static void main(String[] args) throws JsonProcessingException {       ObjectMapper mapper = new ObjectMapper();       Technology tech = new Technology("Java", "Oracle");       Employee emp = new Employee(110, "Raja", tech);   ... Read More

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

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

201 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

Can we override only one method while implementing Java interface?

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 body for all the abstract methods of the interface and obtain the object of the implementing class.Example Live Demointerface Sample {    void demoMethod1();    void demoMethod2();    void demoMethod3(); } public class InterfaceExample implements Sample {    public void demoMethod1() {       System.out.println("This is demo method-1");    } ... Read More

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

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 Java as of now.But you can achieve this using various other methods −Using the toString() method of the Arrays classThe toString() method of the Arrays class accepts a String array (in fact any array) and returns it as a String. Pass your String array to this method as a parameter. ... Read More

Advertisements