Access Fields of an Interface in Java

Maruthi Krishna
Updated on 06-Sep-2019 14:04:49

15K+ 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. By default, All the members (methods and fields) of an interface are public.All the methods in an interface are public and abstract (except static and default).All the fields of an interface are public, static and, final by default.If you declare/define fields without public or, static or, final or, all the three modifiers. Java compiler places them on your behalf.ExampleIn the following Java program, ... Read More

Ways to Initialize Instance Variables of a Class in Java

Maruthi Krishna
Updated on 06-Sep-2019 14:01:45

3K+ Views

You can initialize the instance variables of a class using final methods, constructors or, Instance initialization blocks.Final methodsWhenever 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). You can also use these final methods to initialize the instance variables.Example Live Demoimport java.util.Scanner; public class FinalMethods {    int age = getAge();    String name = getName();    static Scanner sc = new Scanner(System.in);    public static final int getAge() { ... Read More

Util Arrays and Reflect Array in Java

Maruthi Krishna
Updated on 06-Sep-2019 13:57:13

131 Views

The util.Arrays classThe java.util.Arrays class contains a static factory that allows arrays to be viewed as lists. Following are the important points about Arrays −This class contains various methods for manipulating arrays (such as sorting and searching).The methods in this class throw a NullPointerException if the specified array reference is null.For example, the equals() method of this class accepts two arrays and compares them.Example Live Demoimport java.util.Arrays; public class ComparingArrays {    public static void main(String args[]){       String[] myArray1 = {"JavaFX", "HBase", "OpenCV", "WebGL", "FlexBox"};       String[] myArray2 = {"JavaFX", "HBase", "OpenCV", "WebGL", "FlexBox"};     ... Read More

Reflect Array Class in Java

Maruthi Krishna
Updated on 06-Sep-2019 13:53:15

112 Views

The Array class of the java.lang.reflect package provides static methods to create and access Java arrays dynamically. Array permits widening conversions to occur during a get or set operation but throws an IllegalArgumentException if a narrowing conversion would occur.This class provides the newInstance() method, getter method, and setter methods. The newInstance() method accepts an object of the class named Class representing the required component and an integer representing the length of the array, creates and returns an array with the given specifications.Example Live Demoimport java.lang.reflect.Array; import java.util.Arrays; public class Reflection_ArrayExample {    public static void main(String args[]){       Integer ... Read More

Non-Static Blocks in Java

Maruthi Krishna
Updated on 06-Sep-2019 13:49:55

4K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodInstance initialization blocksSimilar to static blocks, Java also provides instance initialization blocks which are used to initialize instance variables, as an alternative to ... Read More

Assigning Arrays in Java

Maruthi Krishna
Updated on 06-Sep-2019 13:03:58

9K+ Views

While creating variables first of all we will declare them, initialize them, assign/re-assign values to them.Similarly, while creating arrays −You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;Assigning values to an arrayWhen we assign primitive values of one type to a variable of other (datatype) implicitly they are converted.But, when you try to assign a higher datatype ... Read More

Defining Generic Method Inside Non-Generic Class in Java

Maruthi Krishna
Updated on 06-Sep-2019 12:59:31

3K+ Views

You can write a single generic method declaration that can be called with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately. Following are the rules to define Generic Methods −All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example).Each type parameter section contains one or more type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies ... Read More

Restrictions While Declaring a Generic Type in Java

Maruthi Krishna
Updated on 06-Sep-2019 12:57:21

1K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Restrictions on genericsYou cannot use generics in certain ways and in certain scenarios as listed below −You cannot use primitive datatypes with generics.class Student{    T age;    Student(T age){       this.age = age; ... Read More

Import Classes from Another Directory Package in Java

Maruthi Krishna
Updated on 06-Sep-2019 12:37:48

7K+ Views

In Java classes and interfaces related to each other are grouped under a package. The package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in the java.io package.There are two types of packages namely user-defined packages and built-in packages (pre-defined)The import keywordWhenever you need to use the classes from a particular package −First of all, you need to set a classpath for the JAR file holding the required package.Import the required class from the package using the import keyword. While ... Read More

Traversing Contents of a Hash Map in Java

Maruthi Krishna
Updated on 06-Sep-2019 12:33:45

744 Views

A map is a collection in Java which stores key-value pairs. The keys of this must not be null and each key should point to only one value. It is represented by the Map interface of java.util package. There are various classes which provide implementation to this interface.The HashMap is a class which implements the Map interface. It is based on the Hash table. It allows null values and null keys.In short, you can store key-value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we use for ... Read More

Advertisements