Tagged: #java

Define an abstract property in java

Abstract keyword is normally applied on classes and methods in order to delegate the implementation of some behavior to subclasses. Java doesn’t support abstract properties, if you try to mark a class property as abstract, you get a compilation error. In this tutorial, we introduce 2 ways for defining abstract properties which are set by subclasses without using the abstract...

Static keyword in java

Every instance of a class has its own state and behavior, whenever the state of a particular instance is modified, other instances are not affected. In java, a static keyword is normally applied to class members or methods in order to make them shared between all instances, the static keyword forces the variable or the method to only have one...

How to use Enums in java

In this article, we provide several examples on the usage of Enums in java. 1. Overview of Enum Enum is a java type/class which holds a fixed set of related constants, it is the replacement of the traditional definition of multiple static final variables in java. It is mostly recommended when you define a method which accepts a constant argument,...

Java IO: difference between absolute,relative and canonical path

The common way of accessing the file system in java is through the java.io.File API. In this tutorial, we explain how to use the File API to access a file using absolute and relative paths, we also describe the difference between getPath(), getAbsolutePath() and getCanonicalPath() methods provided by the API. 1. Absolute vs Relative In general, a path is a way...

How to create an immutable class in java

An object is immutable if its state cannot change after construction, immutable objects don’t expose any way for other objects to modify their state, the object’s fields are initialized only once inside the constructor and never change again. In this article, we define the typical steps for creating an immutable class in java, we also shed the light on the common...

Working with hashcode() and equals() in java

By default, the java super class java.lang.Object provides 2 important methods: equals() and hashcode() for comparing objects, these methods become very useful when implementing large business which requires interactions between several classes. In this article we talk about the relation between these methods, their default implementation and the circumstances which force developers to provide a custom implementation for each of them. 1. Method...

Java – pass by reference or pass by value

Before describing how arguments are passed in java, it is worth to define how java variables are allocated inside the memory. Basically we talk about 2 types of variables: primitives and objects. Primitive variables are always stored inside the stack memory (the memory space which holds method specific variables that are short-lived, in addition to references to other objects in...

Java – Read files from classpath

In this article, we show how to read a resource file from classpath in java. 1. Add file to classpath Before reading the file, you have to add it to the classpath: If the file to be read exists under a specific folder inside the project structure, then just add the parent folder to classpath and its children will be automatically...

Web service project structure

In this article we introduce the most common way for structuring a maintainable web service, our example can be used as a template for creating any web service using object oriented language. The important factor when structuring an application is to divide it into several modules/components and to define how these components interact between each other. The following example is about a java...