Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a class without methods have a special name?

Tags:

java

class

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?

like image 227
lumixel Avatar asked Jun 21 '15 21:06

lumixel


People also ask

Can a class not have a name?

Yes, we can create a class without a name using the Anonymous class. Anonymous class is an inner class which does not have a name and whose instance is created at the time of the creation of class itself and these classes are somewhat different from normal classes in its creation.

Can we define class without method in Java?

Yes, we can declare an abstract class with no abstract methods in Java.

Does each method in a Java class must have a unique name?

Each method in a class must have a unique name. Two (or more) methods can have the same name but must have different parameters. For example, "int getHeight(int x)" and "int getHeight(String s)" can be the names of two methods in a class. This is called overloading of methods.

Can method and class have same name?

Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur.


2 Answers

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

like image 145
Alex Avatar answered Nov 15 '22 12:11

Alex


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.

like image 28
Raman Shrivastava Avatar answered Nov 15 '22 12:11

Raman Shrivastava