Are Java classes objects?

Java classes are not objects, they're an abstraction.

However, each Java class has a corresponding instance of the java.lang.Class class that represents it. That representation is an object. But you shouldn't mistake the representation for the actual thing.

The relationship is somewhat similar to that between music and sheet music. Although the written notation represents music, it is not itself the music.

The difference rarely matters in practice though, so long as you know what you can and cannot do with java.lang.Class objects.


A Java class is not an object.

However, every Java class has an instance of the Class class describing it.
Those instances are objects.


Well, if a Class can understand methods, and have its own atributes (using "static") Then why not think of them as Objects? Objects do that.

But it's not something to bring up to the students, because it will only confuse them. I think that if you already master the concept of Class and object, then you can think of clases as a kind of object.


The class (your code, or even the compiled code in your .class files) are not objects. You don't have an object until you instantiate that class.

For example, Java.lang.String is a class. String s = new String("Hello world"); defines an object of type String. That may be the distinction your professor is making.

Tags:

Java

Oop