5

What is a class without methods called? I'm trying to understand and learn about classes, so i have the following class "Variables" that doesn't have methods, only fields, like the field "Server" in a folder called "utilities":

package com.abc.app.utilities;

public class Variables {
    public static String Server="http://192.168.1.29/"; 
}

then this class is called from a method in another class located in another folder or package this way:

 URL url = new URL(Variables.Server + "...");

is this (the without methods) a particular type of class or is a common class despite not having any method?

lumixel
  • 167
  • 3
  • 11

4 Answers4

11

Raman is right in that all objects inherit the methods of the Object class, so you technically can't have a class without any methods at all.

But if you're talking about a class that doesn't override any of those methods, don't have methods of its own, and most/all fields are public, then people typically call those POD types, or short for Plain Old Data type.

Something like:

public class Point2D {
    public int x;
    public int y;
}

would be considered a POD type

Alex
  • 3,111
  • 6
  • 27
  • 43
2

The 2022 answer is that we now have records to manage our data-classes.

1

There is no class which doesn't have any method. All Classes are subclass of Object class. So all the methods from Object class are inherited. eg. toString(), hashCode() etc.

And there is no special name for them.

Raman Shrivastava
  • 2,923
  • 15
  • 26
1

For Java, there is absolutely nothing special about this class.

For the programmer it is more of a constants class (considering its name and the fact that it's only used to hold a constant), which is not really a good idea to do.

Community
  • 1
  • 1
M A
  • 71,713
  • 13
  • 134
  • 174
  • in the application i have is not so bad idea to use that constant because the ip address is used in several classes so with that constant is not necessary to write the ip every time is needed and also if the ip is changed it only would be changed in that field or constant called Server of the class Variables – lumixel Jun 21 '15 at 22:30