Advantages and Disadvantages of Encapsulation in Java

The word “Encapsulation” means that closing something like we put the main power of the tablet inside the capsule. It is a mechanism that is done to bind the data and protect it from being used by everyone. After doing encapsulation, the variables and the data of one class cannot be used in another class and this data can be used by the member functions of the same class. Encapsulation can be achieved by adding the private specifier to the

Read more

Java Reflection Invoke Static Method

This article will look at how we can use Java Reflection API to invoke a Static method. We will see the steps and the explanation with code. Let us first have a quick look at Reflection API. Reflection in Java is a process of examining or modifying the behavior of a class at run time. Moreover, It is an API or Application Programming Interface having its use in debugging. It is also used in testing tools, with the primary focus

Read more

Java Program for Employee Details using Class and Object

In this article, we will talk about the Java program for employee details using class and object. An Employee is a person or also be referred to as an entity that consists of various attributes such as – emp_id, emp_name, emp_salary, emp_department, emp_email, emp_address, and many more. We use the getter (to receive Employee details) and setter (to set Employee details) method in this program. In the main class, we will create an object of the EmployeeDetails class and with

Read more

Covariant Return Type in Java

In this article, we will learn about covariant return types in Java. Before Java version 5.0, we could not change the return type of an overridden method but a new method was introduced in JDK 5.0, called covariant where we could change the return type of the overridden method. The return type must be non-primitive. (i.e., it should be a sub-type of the overridden class). Program:

Output:

Explanation: In the above program, we created three classes: class ‘Employee’,

Read more

Static Import in Java with Example

In this article, we will discuss static import in Java. Just as import statements are used to access a class without its package qualification. In a similar manner, static imports are used to access static members without their class qualification. Some basic static methods used with the class names are as follows: Math.sqrt(4): It is used to find the square root of the given number. Math.max(4, 2): It is used to find the maximum of the given numbers. Math.min(4, 2):

Read more

Advantages and Disadvantages of Generics in Java

Generics in Java program was introduced in Java version 5. It is used to ensure the data type safety of objects. So, it constructs the code steady by detecting bugs at compile time. Why do we need generics in Java? When we write programs, errors occur. Some errors occur at run time while some errors occur at compile time. Those errors which occur at compile-time can be easily detected and rectified in the program. But, run time errors occur when

Read more

How to Access Variable From Another Class in Java?

In this article, we will learn how to access variable from another class in java. There are two ways to get variables from another class. Create an object of another class in the main class Extend another class in the main class Let us take a look at both these methods one by one with the help of sample programs. Method 1: Create Object of Another Class in Main Class In the following example, to access the variable ‘a’ of

Read more

Java Global Variables

In this article, we will see what is a global variable and how we can use this in our Java programs. In Java, all the functions and data members are declared inside a class and every declaration has to be done inside it. If we declare them outside the class, then they would not be accessible by the object of that class. Therefore, in Java, there is no such precise concept of global variables because these are described at the

Read more
1 2