5

I know that Java classes provide the blueprint for objects.
I also know that all classes inherit from the Object class.

With those facts in mind are Java classes still considered to be objects themselves?

I have searched many resources and am still confused. Many references will say everything in Java (besides primitives) are objects, including classes and your class files... such as here:

http://howtoprogramwithjava.com/podcast-episode-10-objects-and-static-keyword

I searched StackExchange and this question helped a little but didn't completely answer my specific question.

Community
  • 1
  • 1
Torvaldy
  • 79
  • 2
  • 7

6 Answers6

12

Classes themselves are not objects in the sense that there is no runtime object that is directly used in the execution of a class. Other objects exist, however they are representations of the class and changes forced onto them in memory will not change the execution of that class's bytecode. However, every class has a Class object associated with it that allows for interaction with that class, its instances, and members via reflection.

This class is actually generic in that FooClass.class is actually java.lang.Class<FooClass>, which helps out with generics as passing a Class object in can resolve generic return types and constraints at runtime.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • Would you not consider reflection 'manipulation' of a class object? – jtravaglini Aug 16 '13 at 16:51
  • 3
    @jtravaglini No, a reflection uses class objects as *glue* or an abstraction between the JVM's classes and the environment. – nanofarad Aug 16 '13 at 17:13
  • Plus, it can't perform any manipulations -- it can't add or remove methods or fields, can't change visibility, etc. – yshavit Aug 16 '13 at 17:51
  • Actually, they are objects. 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:47
  • @Rogério No, that is actually not correct. `java.lang.instrument` uses `ClassDefinition` for that. There are no methods [here](http://www.docjar.com/html/api/java/lang/Class.java.html) that actively modify the class other than serving as glue to a lower-level API or another object that actually transforms bytecode, not even native methods. – nanofarad Sep 05 '13 at 17:46
  • @hexafraction I only said that "you can change the bytecode ... at runtime", which *is* true. I didn't say that it is done through methods in `java.lang.Class`. If you still think that Java classes are not objects, I believe you should clearly and precisely specify *which* objective criteria you are using. I cannot think of any specific requirement that something needs to satisfy in order to be an object, that a `Class` instance would not satisfy. – Rogério Sep 05 '13 at 18:21
  • @Rogério I am basing this on the fact that a `Class` object does not contain enough information that if all fields were stored then the class could be reconstructed and loaded/instantiated/static methods called/etc. – nanofarad Sep 05 '13 at 18:46
5

Conceptually, a Class is a description of state and behavior. An Object (an instance) is a data structure containing that state and behavior.

For example, given a class

class User {
    String name;
    void setName(String name) {
        this.name = name
    }
}

The class User has behavior and state, ie. it has a Field called name and a Method called setName. The above describes this behavior. If you create an instance

User user = new User();
user.setName("Jon");

you now have a data structure containing actual state and exhibiting behavior.

In Java, you have what is called Reflection which basically describes the metadata of a Class, its state, and its behavior. This is interpreted as instances of Class, Field, and Method classes, respectively.

In the example above, since the field name itself has state and behavior (it has a name ("name"), we can read it or write to it), there is a class that must describe it. The class that describe that state and behavior is Field and instances of Field contain the state and behavior.

Similarly, the Method class describes a method. A method has a state, its name ex. setName, the arguments it accepts, ex. a String. It also has behavior, ex. it returns void (doesn't return anything).

Finally, you have the class Class which describes the state and behavior of a class. Instances of Class describe the state and behavior of a class. For example, the Class instance for the User class will have a Field object and a Method object (it actually has more than that, but bear with me). Fields and methods are state of a class. The behavior is, for example, creating an instance of the class.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

EDIT: Classes are objects as well when they are invoked. Otherwise they are reference types.

For each class definition, the class loader invokes instances of java.lang.Class and stores these special class objects in the PermGen space of the JVM memory.

See here for more info.

Community
  • 1
  • 1
jtravaglini
  • 1,676
  • 11
  • 19
  • Just for completeness' sake PermGen won't be around in Java 8. – nanofarad Aug 16 '13 at 16:48
  • 1
    *Yes, all classes are objects as well.* this is wrong. Check the other two answers. – Luiggi Mendoza Aug 16 '13 at 16:50
  • @LuiggiMendoza the other answers are wrong. – jtravaglini Aug 16 '13 at 16:50
  • Can you show at least one official resource proving that you're right by saying that *a class is an object*? – Luiggi Mendoza Aug 16 '13 at 16:51
  • Does http://docs.oracle.com/javase/tutorial/reflect/class/index.html suffice? – jtravaglini Aug 16 '13 at 16:52
  • A class is not an object, though it does have an object associated with it. A class is, kind-of, a bunch of bytecode, compiled code, header metadata, etc. It's really just a conceptual abstraction. – yshavit Aug 16 '13 at 16:52
  • A class *is* an object. An object is a bunch of bytecode, compiled code, metadata, etc. – jtravaglini Aug 16 '13 at 16:53
  • That link proves that you're wrong: *For every type of object, the Java virtual machine instantiates **an immutable instance of java.lang.Class***. – Luiggi Mendoza Aug 16 '13 at 16:53
  • "Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader." Class *objects* -- I don't see why this is confusing. http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html – jtravaglini Aug 16 '13 at 16:55
  • A `Class` is the same thing as its associated class in the same way that [this page](http://stackoverflow.com/users/1076640/yshavit) is the same as me. Just because there's a 1-to-1 doesn't mean they're the same. – yshavit Aug 16 '13 at 16:56
  • 2
    Because there's a Java class called `Class` and it is `java.lang.Class`. You're misunderstanding it. – Luiggi Mendoza Aug 16 '13 at 16:56
  • 1
    Here's a distinction: a class is something that exists at compile-time as well as at runtime, whereas its associated `Class` object only exists at runtime (as far as a Java app is concerned). Here's another: if you run the same Java app twice without modifying the code, you'll be using the same classes, but they'll have different `Class` objects representing them. – yshavit Aug 16 '13 at 17:03
  • I concede - strictly speaking you are correct. – jtravaglini Aug 16 '13 at 17:08
1

With those facts in mind are Java classes still considered to be objects themselves?

NO, you can not say a java class/type as an object/instance.
object to a class/type means a memory allocation on Heap (where memory depends on member variable defined inside the class). Which indeed will happen when you invoke the type/class Constructor with new Keyword.

However there is a object of class/type Class(java.lang.Class) associated or created when a class is loaded in to JVM by ClassLoader. This Class object is helpful for JVM to keep track of classes running in the JVM and also to acquire a LOCK on static methods when they are synchronized. For a programmer on the other hand, Class object of a class is helpful in many ways.

For example: REFLECTION.(java.lang.reflect)...

As far as Object(java.lang.Object) is just a super class of all the class, that doesn't make the class as objects/instances. But if there is a class called A , as we know it essentially is subclass of java.lang.Object and now any object/instance of class A will have the properties of both class class A and java.lang.Object.

Bottom Line: Every operation inside JVM will happen under the influence of Object Oriented Principle(OOP) hence the saying "Every thing is Objects" except for primitives. For justification if there is a class loaded in to JVM without new keyword, it will also have a java.lang.Class object/instance associated with it.

Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92
0

Classes and Objects are different. But an object is an instance of a class.

The class is used to declare the properites and procedures.

You then create objects based on the class.

Example: You have an Animal Class and a Dog Object

Here is a longer read if you'd like to dig a little deeper.

sealz
  • 5,348
  • 5
  • 40
  • 70
0

This should explain it better; the example simply says, Car is a class. But we can have have many Cars depending on their features/attributes. Objects help us define those. So Classes are not Objects. From Oracle Docs. (Replaced Bicycles with Car, since somehow we all relate to it and better to understand.)

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

The following Car class is one possible implementation of a bicycle:

class Car{

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}
JNL
  • 4,683
  • 18
  • 29