0

I am coming from Java to Objective C, and the idea of a class object has me wondering about similarities with Java. From the Objective C guide in Apple documentation:

A class definition's information is compiled and recorded in data structures made available to the runtime systems. The compiler creates just one object, a class object, to represent the class.

So my understanding is that the class object is created for all classes that are going to be used by the program, and a class object is what is used to create objects for that class.

For comparison, does the JVM have a similar object for all classes it loads?

2
  • 1
    Java and Objective C have many similarities in structure. They both have the concept of a class, and an Objective C protocol is pretty much an Interface. Java was partially based on Objective C (as well as other languages). Commented Dec 18, 2009 at 14:27
  • Java's interface was derived from Objective-C's interface (informal categories and abstract classes more than the primary declaration) which is why Obj-C's interface is named "protocol" instead. :) Commented Dec 18, 2009 at 18:56

3 Answers 3

9

Given that Java was derived directly from Objective-C (no, really, it was), the runtime models of the two are quite similar.

In Java, the notion of a "Class" isn't quite as generic as it is in Objective-C.

In Objective-C, a Class is an instance of what is known as the metaclass. For all intents and purposes, each Class object in Objective-C does exactly as you say; it describes a particular class available in the Objective-C runtime.

The same is conceptually true of Java classes. There is one key difference. In Objective-C, class methods are inherited across subclasses and more significantly a subclass can override a superclass's class method(s).

For example, the NSArray class implements the +array class method (the '+' means "class method"). The NSMutableArray subclass of NSArray overrides +array to return a mutable instance instead.

java.lang.Class is more akin to the Objective-C runtime API; it is the mechanism via which you introspect the classes available in the runtime. Since Java doesn't have functional API, the API is wrapped up in an appropriately named class. java.lang.Class is kinda the runtime API and the metaclass all in one.

Sign up to request clarification or add additional context in comments.

Comments

4

A comparable structure in Java would be java.lang.Class.

1 Comment

Only sort of; java.lang.Class is much more like the Objective-C runtime's API in that it provides the means to introspect the classes available.
1

I think there is a class object for each class.
That class object is the one that, at low level, is used for functions as class_getName(), class_getSuperclass(), class_getVersion(), class_respondsToSelector(). If there would be a single class object for all the classes, then those functions would return the same result for all the classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.