7

During my interview, interviewer asked me

Can we create class without name ?

Since, I was not sure, if it is really possible to create a class without name. So, I said No.

Later, I tried Googling and found, others are also looking for the answer of the same question, but I didn't found clear answer.

I will appreciate, if anyone clearly explain about this class. I mean, what that class technically known as and how can we instantiate this class ?

Ravi
  • 30,829
  • 42
  • 119
  • 173

3 Answers3

12

Yes, it's called an anonymous class/struct.

In C++:

class {
} x;

x is an object of the type, and you can't create any more, because, well, how would you, given that the class doesn't have a name and all....

how would one call constructor and destructors

You don't. In both Java and C++ constructors and destructors hold the same name as the class (they're not PHP - __construct or whatever), and the missing name kind of gets in the way.

Joshua Kaiser
  • 1,461
  • 9
  • 17
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    Such a class cannot have user-declared constructors. – Robᵩ Oct 22 '12 at 14:36
  • which means we can't create any object of that class... ?? – Ravi Oct 22 '12 at 14:38
  • Actually, it seems you can create a copy using `auto` in C++11. – juanchopanza Oct 22 '12 at 14:39
  • please provide some good links, where i can learn more about this class... i mean.. why do we need such type of class.. and etc.. – Ravi Oct 22 '12 at 14:41
  • @coders I don't have a binder of links - just google for anonymous class - now that you know what it's called, it should be easy. – Luchian Grigore Oct 22 '12 at 14:43
  • @coders If you don't know why you'd use such a construct, why would you ask if it exists? – Cubic Oct 22 '12 at 15:07
  • 2
    The C++03 Standard actually refers to these as "unnamed" classes, not "anonymous". You can `typedef` such a construct, as with `typedef class {} Thingy;` thereby making it easier to instantiate such things: `Thingy t;` But then, if you're doing this in C++, I'd wonder why you really want it to be unnamed in the first place. Perhaps to make declaration of a non-trivial constructor impossible? Hmmm... – John Dibling Oct 22 '12 at 15:09
  • @JohnDibling that would make a good question IMO (but I think you already answered it). – Luchian Grigore Oct 22 '12 at 15:10
  • @LuchianGrigore: I've actually never thought of creating such a construct before. I'm pondering the possible uses. – John Dibling Oct 22 '12 at 15:12
  • it could have an assignment operator but that couldn't return a reference to the class nor could it take the class const reference as its parameter. – CashCow Oct 22 '12 at 15:44
  • A comment on the downvote is always welcomed. I want to know if I'm wrong. – Luchian Grigore Oct 23 '12 at 08:16
6

Its also called an anonymous class in Java.

// create a new instance of an anonymous class.
Serializable s = new Serializable() {
};

Note: In the JVM, all classes have a name, it's generated by the compiler for you.

You can't define constructors, but it can have an instance initializer block which does much the same thing.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Do you mean an instance initializer block? – Jon Skeet Oct 22 '12 at 14:38
  • @JonSkeet Thank you, anonymous classes can't have static initializer blocks. ;) – Peter Lawrey Oct 22 '12 at 14:40
  • Descriptions of anonymous classes always seemed off to me. The class always has a name, instance may not. – jn1kk Oct 22 '12 at 14:45
  • IMHO, instances don't have names unless you add a field like `name`. Class has a given name or it is anonymous (in which case the compiler gives it name) – Peter Lawrey Oct 22 '12 at 14:50
  • *In the JVM, all classes have a name*, technically you can have a class w/ an empty string as name. I have not tried `null`, `ClassLoader.checkName(String)` considers `null` fine but I am not sure about the JVM code (and I am too lazy to dig it) – bestsss Oct 27 '12 at 20:11
0

In java, you can create "anonymous inner classes", for a detailed answer see How are Anonymous (inner) classes used in Java?

Community
  • 1
  • 1
Kim Sullivan
  • 943
  • 2
  • 10
  • 15