Found 2620 Articles for Java

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

Maruthi Krishna
Updated on 15-Oct-2019 06:50:25

6K+ 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 make elements of array immutable in Java?

Maruthi Krishna
Updated on 15-Oct-2019 06:44:33

2K+ 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.Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class UnmodifiableExample ... Read More

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

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

275 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 wouldn't allow initialization of static final variable in a constructor?

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

532 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

582 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

2K+ 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

Advertisements