12

I've read before that Java classes are instances of the class Class. But now, my computer science teacher says that Java classes are not objects.

Which is true?

itdoesntwork
  • 4,666
  • 3
  • 25
  • 38
  • What reason did he give you when you asked him? – user207421 Aug 17 '12 at 00:24
  • @EJP He said something about a class being an interface, though I don't remember the whole thing. – itdoesntwork Aug 18 '12 at 14:07
  • 1
    Java classes are indeed objects, even if a special kind of object. You can, for example, have multiple instances of any given class `SomeClass`: just create a new `ClassLoader` and load "SomeClass" again; you get a different instance for the same compile-time class, potentially with different method implementations. – Rogério Aug 16 '13 at 19:00

5 Answers5

22

A Java class is not an object.

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    I think it matters what you mean by "a Java class." What definition of "class" isn't an object? The source code with the class declaration in it? – Matt Ball Aug 16 '12 at 15:12
  • So the instance of the class `Class` is not the same thing as the class created with the `class` keyword. Got it. – itdoesntwork Aug 16 '12 at 15:16
  • @MattBall: A class is a type, not an object. – SLaks Aug 16 '12 at 15:47
  • 1
    So what is `String.class` a literal representation of? – Matt Ball Aug 16 '12 at 17:01
  • It is an object. Instances of java.lang.Class use memory and can even be garbage collected (when loaded through a custom classloader which eventually becomes garbage). They have instance fields (such as String name), and many instance methods. You can even change the bytecode of method/constructor bodies at runtime, for any loaded class, at any time (certain mocking and code coverage tools do just that, using the java.lang.instrument API). – Rogério Aug 16 '13 at 18:53
8

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.

biziclop
  • 48,926
  • 12
  • 77
  • 104
1

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.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
1

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.

Fabrizio
  • 11
  • 3
  • 1
    I do not think static method are part of the objects. They seem to be part of the class only. – Scratte Aug 04 '20 at 18:53
  • @Scratte What I'm saying is that when you define static methods and attributes in a Class you can talk to the class directly instead of talking to instances of the class, that's why classes "look like objects" to me. – Fabrizio Aug 06 '20 at 12:19
0

Every Java class, even java.lang.Class descends from java.lang.Object.

EDIT:

The wording is a bit ambiguous. The instances of Java classes are definitely objects. Classes by themselves, cannot be really considered objects, well because nothing exists in memory except for the class "blueprint".

jn1kk
  • 5,012
  • 2
  • 45
  • 72
  • 1
    Class objects do exist in memory. Consider the result of Object.getClass(). – user207421 Aug 17 '12 at 00:25
  • @EJP not sure what is your point. getClass() only works when you have an instance of that class, yes. I never said otherwise. My point is unless you have an instance, there is nothing in memory, only a mapping of classes in JVM (blueprints). – jn1kk Aug 17 '12 at 00:43
  • @EJP I find it silly that we are arguing and taking away other people's imaginary internet points because the question itself is ambiguous. I find it even more silly I am even trying to answer this question. – jn1kk Aug 17 '12 at 00:52
  • Consider the result of MyClass.class. No instance needs to exist, but it still returns an object. There is nothing silly about correcting misinformation, and I don't know what the part about taking away other people's points refers to: I haven't voted on anything in this thread. – user207421 Aug 21 '12 at 05:25