Is there any special jargon word for a class that has no functions but is used to store data?
-
I couldn't understand why this question was closed. If someone don't know something, can't he ask it from the community or experts...damn... – RoboAlex May 07 '13 at 16:48
-
1Here's why I voted, from the close reason: `this question will likely solicit debate, arguments, polling, or extended discussion`. There were 11 answers total, 7 of them with one or more downvotes, 3 answers deleted, and 2 answers with 10 or more comments. I'd say there's been more than a fair amount of debate, arguments and extended discussion. – LittleBobbyTables - Au Revoir May 07 '13 at 16:51
-
@LittleBobbyTables So what is the problem with the questioner or the question? I asked what I don't know and wanted to be known for my work. – RoboAlex May 07 '13 at 16:54
-
2If you feel it was improperly closed, you can flag it for moderator attention under the "Other" option and asked that it be considered to be re-opened, or ask on [Meta]. – LittleBobbyTables - Au Revoir May 07 '13 at 17:03
-
Isn't it better to have a structure rather than a class that has no methods and only store data? – Azhar Khorasany May 07 '13 at 14:07
-
The decision as to whether to use a class or a struct is completely unrelated to whether or not there are methods. It's perfectly appropriate to have structs that have a number of methods (in some contexts), just as it's appropriate to have a class with nothing but data (in some contexts). This is a non factor. Also, by the way, this doesn't technically answer the question that was asked. – Servy May 07 '13 at 14:46
7 Answers
One of the examples is Data Transefer Object (DTO), although it, of course, still can have methods.

- 16,038
- 10
- 74
- 104
-
1
-
Also not the downvoter, but that's an example, not the overall term for such a class. – LittleBobbyTables - Au Revoir May 07 '13 at 13:49
-
-
1
Plain old data structure (POD) seems to be an appropriate term. Though rarer than POJO/POCO, from what I've seen, it seems to be the best fit for your criteria.

- 55,448
- 7
- 96
- 122
Do you mean something like this?
public class Foo {
public int a;
public String b;
}
I don't think there's a specific term for a (public) class like that in Java. Except maybe "bad practice".
If your platform has a decent JIT compiler, there's no good reason to write code like that. At least make the fields private and provide getters and/or setters. A decent JIT compiler will optimize simple getters and setters so that there is no performance overhead.
The key point is that you should never let code like that appear in a API that is exposed outside of a single compilation unit. Why? It exposes the implementation details of the class and forces other code to depend on them.
If the class is an private inner class the above code could be reasonable, though I'd be more comfortable if the fields were final and there was a constructor. Especially if the compilation unit was large.

- 698,415
- 94
- 811
- 1,216
-
There is, actually: in many contexts (local classes, for example) this saves lines of code with no harm done. – Marko Topolnik May 07 '13 at 13:52
-
1@MarkoTopolnik - Did you see the 'public' modifier on the class? That's why it it bad practice!!! – Stephen C May 07 '13 at 13:53
-
1
-
-
Yes, for brevity's sake I'd omit `public` as well, but not to constrain access---for example I don't bother to put `private` where the class's reach actually is just private. Anyway, OP didn't specifically ask about public classes. – Marko Topolnik May 07 '13 at 13:55
-
The problem is that it is not clear what he's *really* asking. Anyway, my Answer is clearly qualified by the first sentence. – Stephen C May 07 '13 at 13:59
-
OP may in fact have even had classes with accessor methods in mind, as distinct from actual *functions* that compute something. – Marko Topolnik May 07 '13 at 13:59
-
2@StephenC `var foo = JsonConvert.DeserializeObject
(@"{""a"":1,""b"":"x"}");` Do you know better way other than accesing the json properties dynamically. – I4V May 07 '13 at 14:00 -
That doesn't look like Java syntax. I'm specifically talking about a Java example. – Stephen C May 07 '13 at 14:06
-
@StephenC Syntax isn't important. It just deserializes the json string into class `Foo` – I4V May 07 '13 at 14:07
-
3
-
@I4V - translate your example into Java then. I haven't a clue what it means. – Stephen C May 07 '13 at 14:09
-
@StephenC I would if I could. I am not java developer. Anything you don't understand in this syntax? Just focus on what It does. not how it does. It magically reads the string and returns a Foo object – I4V May 07 '13 at 14:10
-
@I4V Please don't bother making a general point when this answer is specifically in the context of Java. Java is not a dynamic language and most people get very upset when they see "naked" object properties. – Marko Topolnik May 07 '13 at 14:12
-
@I4V - yep. Like what it is actually supposed to do. And whether it would make sense to try to do it in Java. – Stephen C May 07 '13 at 14:12
-
-
@I4V - no. But THIS Answer is clearly talking about the Java part of the question. If you want to make points about C#, make them on a different Answer. They are not relevant to THIS Answer. – Stephen C May 07 '13 at 14:14
-
@I4V If you knew Java and C#, you'd immediately recognize Stephen's code as being Java and not C#. – Marko Topolnik May 07 '13 at 14:15
-
-
2@I4V There's more to a language than compiler errors. Such as conventions, for example. – Marko Topolnik May 07 '13 at 14:17
-
1@StephenC Not meaning to dispute you, but have you actually ever found yourself bitten by Swing's `GridBagConstraints`, `Point`, or any other example of Java's own class with public fields? I, for one, never, even after more than a decade of heavy usage. – Marko Topolnik May 07 '13 at 14:28
-
I think that the fact that the fields are `final` saves them. But I think you'd agree that those classes would be more flexible if that had getters rather than exposed fields; e.g. you'd be able to subclass them and override the get behaviours. Anyway, they are like that because they first appeared BEFORE there were decent JIT compilers. – Stephen C May 07 '13 at 14:33
-
At least GBC's fields are not final (not the only example). About subclassing, no, I was never even close to wanting to subclass them. That would overall be a bad practice, anyway. Really, in the *extreme* majority of cases, a getter is just a getter and a setter is just a setter, and if it was anything else, that would cause infinite confusion to the user. – Marko Topolnik May 07 '13 at 14:56
-
Still, if you are writing a public API, and are about to be bound forever to whatever choices you make, then clearly you don't want public properties in that API. But who of us writes public libraries? And even those that do, it is at most 10% of their overall code output. In those smaller-scale codebases is where I routinely use naked fields, if it suits my purpose. – Marko Topolnik May 07 '13 at 15:02
-
Perhaps that's the difference. With small-scale codebases and (I expect) just one developer, these things are less important. – Stephen C May 07 '13 at 15:18
There is no standard term for C# because this practice is pretty rare. I call such classes (or structs) "records", for no particularly good reason.

- 647,829
- 179
- 1,238
- 2,067
I see a lot of flamewars and a high rated incorrect answer here. So I'll chime in with my not entirely correct but close enough answer.
A JavaBean is a special data encapsulation object in Java. In C# I'm not entirely aware of the name but they do have a structure (rather than a class) which I'm accustomed to using for similar types of tasks.
Another term you may wish to use is Entity. Java has "persistance entities" which are effectively JavaBeans with an annotation. My advice would be to be consistent with whichever you choose to use.
As mentioned this isn't a perfect answer but it should be close enough.
http://en.wikipedia.org/wiki/JavaBeans
http://docs.oracle.com/javaee/5/tutorial/doc/bnbqa.html
http://msdn.microsoft.com/en-us/library/vstudio/ah19swz4.aspx
http://msdn.microsoft.com/en-us/library/system.data.entity(v=vs.103).aspx

- 645
- 4
- 24
I mostly refer to them as container classes. Maybe the term you're looking for, because it doesn't sound very functional. But they often have getters/setters.
Utility class is also a nice term. Utility class which stores xyz data for use with bla.

- 4,929
- 4
- 40
- 80
Your question is tagged C# and Java, but I've found most people understand if you call them structs (from C).
Note that in C++, structs may have functions too, but I don't think this is idiomatic.

- 2,671
- 21
- 31
-
1I expected this since it's my first answer, but could I get some feedback please? :) – bsa May 07 '13 at 14:00
-
1In my opinion your answer is perfectly fine. Look at my deleted answer, it says about the same thing and it got a downvote as well---with no explanation, of course. – Marko Topolnik May 07 '13 at 14:02
-
A struct is not a class, and as you mentioned structs can have methods. – juharr May 07 '13 at 14:02
-
3@juharr Your comment just shows your own ignorance. Nobody ever claimed that the word "struct" means the same thing it would mean in C. It's called *analogy*. – Marko Topolnik May 07 '13 at 14:04
-
-
@MarkoTopolnik So you want to call a class without methods a struct even though there is a construct known as a struct in the language. That would get confusing very quickly. – juharr May 07 '13 at 14:07
-
1juharr - there's no such thing as a method in C++. They're called functions ;) – bsa May 07 '13 at 14:08
-
1@juharr I'm a Java guy; you are obviously a C# guy. This is very confusing because OP tagged it both, so the worlds are colliding :) In Java the term "struct" is undefined. – Marko Topolnik May 07 '13 at 14:09
-
I didn't know C# had structs though - so yes, that would just be plain confusing. – bsa May 07 '13 at 14:10
-
1For the record, @bsa, `struct` in C# is just the same as `class`, but with different defaults (for stuff with no modifiers on it). – Marko Topolnik May 07 '13 at 14:26
-
@MarkoTopolnik Um...no. There is a *much* bigger difference between a struct and a class than just the default access modifiers in C#. Perhaps you meant to say C++? In C++ the difference between the two is not particularly dramatic. – Servy May 07 '13 at 14:43
-
@Servy So, for example, a `struct` cannot be heap-allocated like the class? If that is correct, then I am misinformed. – Marko Topolnik May 07 '13 at 14:51
-
@MarkoTopolnik Clearly you are mis-informed, but the differences cannot be summarized in a single sentence, or a full SO comment. Your previous statement is not correct, although it does tell me that you aren't familiar with what a struct truly is in C#. I suggest doing some research on the subject. [Here](http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx) is one place you could start. – Servy May 07 '13 at 14:55
-
@Servy `struct` being a value type, whereas `class` being a reference type does a pretty good job for me. My recollection was that `class` and `struct` just had different defaults for this property and that either could be used both ways. – Marko Topolnik May 07 '13 at 14:59
-
@MarkoTopolnik That is true in C++, (except that in C++ they're both value types, even though they can be referenced) but not at all true in C#. – Servy May 07 '13 at 15:00
-
@Servy So apparently [this answer, with score 243](http://stackoverflow.com/a/13275/1103872), is just plain wrong? – Marko Topolnik May 07 '13 at 15:05
-
@MarkoTopolnik Nope. What about that answer in any way disagrees with anything that I've said? – Servy May 07 '13 at 15:07
-
@Servy You are in agreement with the answer only if by "That" in "That is true in C++..." you are referring only to "my recollection" and not to my conclusion (the first sentence). This is not at all the obvious way to understand your comment, though. – Marko Topolnik May 07 '13 at 15:49
-
@MarkoTopolnik I was referring to your second sentance with that phrase. – Servy May 07 '13 at 15:51