Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a class without fields have a special name?

Tags:

java

class

What is a class without fields called? I'm a beginner in programming Java and I'm trying to understand and learn about classes, so i have the following class "DalUser" that doesn't have fields, only methods, like validateSession in a folder called Dal:

import com.app.Be.BeUser;

public class DalUser{    
  public BeUser validateSession(String user, String password)

   {

    ...

   }

I have a class BeUser that has the fields user and password and is located in another folder or package called Be. is this a particular type of class or is a common class despite not having any fields?

like image 247
lumixel Avatar asked Jun 19 '15 21:06

lumixel


2 Answers

What is a class without fields called?

There is no universally applicable name for this1:

  • Classes with no fields have no explicit state, but strictly speaking, every Java object has state associated its mutex. Hence calling them "stateless classes" is a bit of a stretch.

  • Classes with no fields may be "helper classes", but the absence of fields is neither a necessary or sufficient precondition.

  • An instance that has no state is also immutable, so you could call Classes with no fields an "immutable classes" ... though the locking use-case applies here too.

Another distinction between helper classes and stateless classes is whether the class is designed to be instantiated. A helper class (in normal usage) consists of static methods, and is not instantiated. (An instance serves no purpose). A stateless class is often designed to be instantiated, and passed around to other classes which will actually make method calls on the instance; e.g. as a "policy object".

Then there is a sub-case of the "base class" use-case where there are no fields in the base class. (In that case, calling the class "stateless" is misleading, since there is typically state in the child classes.)

In short, you need to examine the class and how it is actually being used to determine which label (or labels) best apply to it.


In your specific example, the code is best described as a stateless class. That is because it is designed to be instantiated and passed around in different contexts, but the functionality does not depend on any state of the object itself.


Here are some other examples to illustrate why there is no simple answer to this.

1) This is a helper class, but it has a (static) field.

 public class Plumbing {
     private static nosTapsFixed;
     private Plumbing() { }
     public class fixTap(Tap t) {
         // fix 't'
     }
 }

2) This is a base class. It has no fields, but it is clearly not intended as a helper class.

 public abstract class Tradesman {
     // no fields
     public abstract Invoice issueInvoice();
 }

3) Here is a use of a class with no fields (java.lang.Object) in a way that is clearly not a "helper".

 final Object myLock = new Object();
 ...
 synchronized (myLock) {
     ...
 }

4) And here is another example of a class that has no fields but is and not a helper.

 public enum Agreement {
     YES, NO
 }

1 - But if you really want a name, how about a "villein class". Villeins didn't own any fields ....

like image 99
Stephen C Avatar answered Sep 17 '22 19:09

Stephen C


These are called helper classes or utility classes, although the methods are usually declared static. Examples include java.util.Arrays and java.util.stream.StreamSupport. Often, they might have a pluralized name (for example, a helper class that works with Widget objects might be called Widgets.

like image 30
nanofarad Avatar answered Sep 18 '22 19:09

nanofarad