Methods in CLIPS
Java supports methods defined on class types. Here’s an example:
To run this program:
In Java, methods are always defined as part of a class. There’s no distinction between pointer and value receiver types as in some other languages. All non-primitive types in Java are reference types, so method calls always operate on the same instance of the object.
Java uses the this
keyword to refer to the current instance of the class within instance methods. This is similar to the receiver in some other languages.
Unlike some languages, Java doesn’t have separate concepts for structs and classes. Everything is a class, and classes can have both data (fields) and behavior (methods).
In this example, we’ve created a Rectangle
class with width
and height
fields, and area()
and perimeter()
methods. The main()
method demonstrates how to create an instance of the class and call its methods.
Next, we’ll look at Java’s mechanism for defining abstract types: interfaces.