Category Archives: Core Java

We will discuss commonly asked questions related to java 8 , exception handling, string operations, java io etc.

Concurrency – Synchronize resources using ReentrantLock in java (example)

Given shared resources in java. Synchronize shares resources using Lock interface. We will use ReentrantLock to protect share resources. A lock is a tool for controlling access to a shared resource by multiple threads. Lock implementations provide more extensive locking operations than can be obtained using synchronized methods and statements. We create five threads, which… Read More »

Concurrency – Producer & Consumer in java (Wait notify example)

Given multi-threading application in java. Create Producer & Consumer threads. Producer thread will produces the data. Consumer thread will consumes the data. Synchronize Producer & Consumer thread using wait & notify methods. 1. DataStore class: DataStore class contains LinkedList to store integer values. DataStore class contains two synchronized methods i.e. get & put put method:… Read More »

Get all fields/data members of class in java (example)

Given a class in java containing public,private & protected fields. Get/list/print all fields of class using class Class. We will use getDeclaredFields() of Class to retrieve all data members. 1. Program: print/list all fields/data members of class in java (example) package org.learn.classes; import java.lang.reflect.Field; class Student { private String name; private int standard; protected String age; public… Read More »

Print/list all methods/functions of class in java (example)

Given a class in java containing public,private & protected methods. Get/list/print all methods of class using class Class. We will use getDeclaredMethods() of Class to retrieve all methods. 1. Program: print/list all methods/functions of class in java (example) package org.learn.classes; import java.lang.reflect.Method; class Person { private String name; private String age; private float salary; public Person()… Read More »

Print/list public/protected/private constructors of class in java (example)

Given a class in java containing public,private & protected contructors. Get/list/print all constructors (prive,protected & public) of a class using Class. getConstructors method of Class will list all public constructors of given class. getDeclaredConstructors method of Class will list all private, protected & public constructors 1. Print/list public/protected/private constructors of class in java (example) package… Read More »

Page 1 of 1912345...10...Last »