In empty class (without field and properties) also compiler automatically creates a default constructor after compiling. If we never use this empty class then why compiler creates this default constructor?
Asked
Active
Viewed 864 times
2
-
Maybe for some custom exception, for the "why the compiler" => because of the JLS, see http://stackoverflow.com/questions/4488716/java-default-constructor – May 09 '17 at 04:43
-
A marker interface, like [`RandomAccess`](http://docs.oracle.com/javase/8/docs/api/java/util/RandomAccess.html), is like this, although I don't think that's exactly what you had in mind since it's not a class per se and doesn't have a constructor. – Radiodef May 09 '17 at 04:45
-
About empty classes: http://stackoverflow.com/questions/28090223/can-i-have-an-empty-java-class – May 09 '17 at 04:46
-
2Besides being mandated by the Java Language Specification: The compiler does not know whether you use this empty class or not - the class could be defined (and compiled) in project A and a subclass of it in a different project B – Thomas Kläger May 09 '17 at 05:35
-
1it must not be a subclass in a different project, it may just instanciated in a second project – user85421 May 09 '17 at 08:12
1 Answers
1
We can use it as type token e.g:
class DatabaseColumnName {}
class DatabaseTableName {}
addItem(DatabaseColumnName.class, "Age")
addItem(DatabaseTableName.class, "Person")
...
getItemsByType(DatabaseTableName.class)
Also, read about usages in other languages to have deeper understanding of empty classes:

Igor
- 2,039
- 23
- 27