Home » Core Java » class

class

Static clause initialization

This is an example of how to make a static clause initialization. In order to do so, we have created two classes as described below: We have a class A that has a method func(int val) that prints the given int value. We also have a class B, that has two static A objects and in a static clause creates ...

Read More »

You cannot have static variables declared inside a method

In this example we shall show you the reason why you cannot have static variables declared inside a method. The steps of the example are described below: We have a class, StaticVar that consists of a process() method, where it sets a static int variable and prints it. We make a new instance of the StaticVar in a main() method and ...

Read More »

Static fields inheritance

This is an example of static fields inheritance between two classes. The steps of the example are described below: We have created a class Aclass that has a static int val, and a static method fun() that returns an int value. We have also created a subclass of Aclass, that is InheritanceStaticFields and extends the Aclass. We create a new ...

Read More »

You cannot override a private or a private final method

This is an example explaining why you cannot override a private or a private final method. The example is described in short: We have created three classes, WithFinals, OverridingPrivate that extends WithFinals and OverridingPrivate2 that extends OverridingPrivate. All three classes consist of a public final f() method and a private g() method. We create a new instance of OverridingPrivate2 class. We ...

Read More »

You cannot override a method just by using the same name

In this example we shall show you why you cannot override a method just by using the same name. We are using two classes as described below: Class A has a method, char func(char a) that returns a char value. It also has a method, float func(float f) that returns a float value. Class Bart extends A and has a method, ...

Read More »

Use inner class for callbacks

With this example we are going to demonstrate how to use inner class for callbacks to a class. The example is described in short: We have created an interface, Incrementable and a class to implement it, Callee1. It has a method increment(), that increases an int value.  We have also created a class, MyIncrement that consists of a method increment() ...

Read More »

Static inner class example

This is an example of how to use a static inner class. We have set the example as described below: We have created a class, ArAlgo that contains a static inner class P. Class P has two double attributes and their getters. ArAlgo class also has a static method, P min_max(double[] vals). The method gets a double array and for ...

Read More »

Anonymous inner class constructor

This is an example of how to use an anonymous inner class. We have set an example following the above steps: We have created an abstract class, Abs that has a constructor and an abstract method. We have also created another class, InnerClassConst, that has a static method, getAbs(int i), that returns a new Abs, for a given int value, where ...

Read More »

Inner class reference

With this example we are going to demonstrate how to make an inner class reference. In short, to make an inner class reference we have followed the above steps: We have created a class InnerClassRef, that contains an inner class C and another inner class D. Class C has a method value() that returns an int value. Class D has ...

Read More »

Creating inner class example

This is an example of how to create an inner class. In short, to create an inner class we have performed the following steps: We have created a class CreatingInner that contains two inner classes, Contents and Destination. Contents class has a method value() and Destination class has a constructor using a String field and a method readLabel(). CreatingInner class ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns