Home » Core Java » class » Page 3

class

Final class fields example

This is an example of how to use final fields in a class, so as to understand the difference between final and non final fields in a class. In short the example is described below: We have created a class FinalFields that has final attributes, such as typical public constants and compile-time constants (private). We create a new instance of ...

Read More »

Final arguments to function

In this example we shall show you how to use final arguments to a function. To use final arguments to a function we have performed the following steps: We have created class FinalArgs, that has run() method and a myFunc(final Calendar d) method. The first method calls the second one, that uses a final Calendar argument and changes the Calendar.YEAR field. ...

Read More »

Full Initialization process

This is an example of a full initialization process in a class. To initialize a class and its fields we have performed the following steps: We have created a class, A, that has a private int x field and a protected int y. It also has a private static int i field that is initialized with a static method print(String), that ...

Read More »

Static value initialization

With this example we are going to demonstrate how to make static values initialization in classes. In short, to make static values initialization in classes we have followed the steps below: We have created a class A with a method function1(int i). We have also created class B, that has two static A fields, b1 and b2. In its constructor ...

Read More »

Access outer variables example

This is an example of how to access outer variables in a class. The example is described in short: We have created a class, OuterVars that has an int field cnt, an inner class Cnt, and a method go().  Cnt class has a field cnt, a method increment() that increases its cnt and the cnt field of OuterVars. It also has ...

Read More »

Null safe equals method

In this example we shall show you how to use a null safe equals method to compare objects. To use a null safe equals method to compare objects we have performed the following steps: We have created an abstract class, ObjectUtils that has a static method boolean nullSafeEquals(Object o1, Object o2) that determines if two objects are equal. If the two objects ...

Read More »

Polymorphism and constructors example

With this example we are going to demonstrate the polymorphism of a class and the constructors behaviour. In short, to see how constructors are used in a class and the changes that a statement can cause to a class we have performed the following steps: We have created an abstract class A, with an abstract method func(), that it uses in its ...

Read More »

Copy Constructor example

This is an example of how to create a copy constructor in a class. We have set the example, in order to demonstrate the copy constructor and its use between classes and their subclasses following the steps below: We have created FruitQualities class, Seed class and Fruit class, that all have copy constructor.  We have also created Tomato class, that extends ...

Read More »

Exceptions in Constructors

In this example we shall show you how to handle exceptions in constructors. To handle exceptions in constructors we have performed the following steps: We have created a class, InputFile, that has a BufferedReader field. In its constructor, it gets a String and it creates a new FileReader with the given String name of path to file to read from. A FileNotFoundException might ...

Read More »

Conflicting Constructors

With this example we are going to demonstrate how to use conflicting constructors in a class. In short, to use conflicting constructors in a class we have followed the steps below: We have created a class ConflictingConstructors, that has a constructor without fields and a constructor that gets an int value and throws an IllegalArgumentException if it is smaller than ...

Read More »