Home » Core Java » class » Page 5

class

Implementing Cloneable interface

In this example we shall show you how to implement the Cloneable interface. A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. To implement the Cloneable interface we have performed the following steps: We have created a class, Employee that implements the ...

Read More »

Clone class example

This is an example of how to create a clone class of a class. We have created Employee class to get its clone class, as shown below: The class Employee has two String fields and a Double field and getters and setters for the fields. It overrides the clone() method of Object, where it creates a new Employee object and ...

Read More »

Find a file in classpath

In this example we shall show you how to find a file in the classpath. To find a file in the classpath we have created a method, File findFileOnClassPath(final String fileName) that reads a fileName and returns the File. The method is described below: It uses the System.getProperty(String key) to find the classpath of java and the path separator used. It ...

Read More »

Get Classpath example

With this example we are going to demonstrate how to get the classpath using the System class. The System class contains several useful class fields and methods. Among the facilities provided by the System class are standard input, standard output, and error output streams, access to externally defined properties and environment variables, a means of loading files and libraries, and ...

Read More »

A wrapper class

This is an example of how to create a wrapper class. A wrapper class is defined as a class in which a primitive value is wrapped up. Here, we create a wrapper class for an int value: We have a class IntVal, that has an int field, getter and setter for the field, a constructor using its field and a method, ...

Read More »

How the class loader works

In this example we shall show you how the class loader works. To see how the class loader works we have performed the following steps: We have created three classes, A, B and C, that each one uses a static block to print a message. The code in a static block is executed when the class is loaded by the class ...

Read More »

Immutable object example

With this example we are going to demonstrate how to create and use an immutable object. Immutable objects are objects whose state cannot change after construction. In short, we have created an immutable object, as described below: We have created a a class, ImmutableObject, that has an int field. It has a constructor using its field. The class has a ...

Read More »

Modify Immutable Objects

This is an example of how to modify immutable objects. Immutable objects are objects whose state cannot change after construction. We have created two classes Mutable and ImutableObjectsExmp, that have the same field, but one is mutable and the other one immutable: Mutable class has an int field, integer. It has a constructor using its field. It has a method, Mutable ...

Read More »

Reference Objects example

In this example we shall show you how to create and use Reference Objects. A reference object encapsulates a reference to some other object so that the reference itself may be examined and manipulated like any other object. Three types of reference objects are provided, each weaker than the last: soft, weak, and phantom. Each type corresponds to a different level of ...

Read More »

Passing an object to a method example

With this example we are going to demonstrate how to pass an object to a method. In short, we have created a class and a method to pass the object of the class: We have created a class, Character with a char field. We have created another class, PassObject, that has a static method, void setCh(Character y). The method gets a ...

Read More »