Can a Java class be an object and object be a class?

Viewed 3309

Can a Class be an Object in Java?

I consider a Class to be a blueprint for creating new Objects, but at the same time classes can have static variables and static methods which are called without an instance of class(object).

And on the contrary, can an Object be a Class?

6 Answers

I consider a class to be a blueprint for creating new objects

Correct.

but at the same time classes can have static variables and static methods which are called without an instance of class(object).

this doesn't make it an object. this is simply because sometimes, you want to have variables & method that are common to all objects. meaning we don't need a different thing for each instance of a class.

And on the contrary, can an object be a class?

no, because an object is a thing not a template.


if you find a function/method that is not going to change across instances of the class then it probably makes sense to make it static.

Yes, and no.

No, in the sense that objects are created from classes, so obviously they are not objects. The static members of the class just belongs to the class. Who says only objects can have members?

Yes, in the sense that you can create an object representing a class, using the Class class. That sounds pretty meta, doesn't it? Basically the Class class represents a class. All the information (name, methods, fields, constructors...) in the class can be acquired from the corresponding Class object. You can then use these information to do reflection. With reflection you can call methods dynamically, set private fields' values and lots of other cool stuff. And you can even create objects from a Class object!

Can class be an object in Java?

No actually. It cannot be an object but it is where one can describe the behaviour of the object. One can explain how a Car should be but they can't be treated as Car right ?

And on the contrary, can an object be a class?

No. Because at any point of time if you see, object have a state and not the class. In object all of its properties have values that you given or that are defined by default.

Probably the below images shows some visual difference

enter image description here source: wikipedia

Since static things are not related to state, you are adding them to Class and not to the object.

enter image description here

As you mentioned you could imagine a class as a blueprint for an object. When the object is created, the jvm creates some dedicated memory in the heap to store the object and its values. While executing programm code, the jvm execute the code which is given in the methods.

static variables are memory which is not directly related to one object instance, they are globaly accessible by everyone (if they are public) so they are handled not directly with the object instance memory. (but keep in mind - static variables are also object instances of the variable type ;-))

static methods are code which could be executed without having a object instance so if they are public everyone could do this.

to give you some idea of the purpose of that stuff:

  • object instance is used to manage one or many instances of a specific type.
  • static methods are often used to provide some common functionality (see java https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html for example)
  • static variables are used to share a variable between multiple instances for example having a logger or something

using static stuff has to handled carefully cause it could "destroy" many oop principles. But if you think about functional programming it could be very usefull.

What is a class?

A class is a blueprint for objects: it describes how a certain kind of objects look (what data the objects made out of the class have and what operations you can do on that data).

What is an Object?

An Object is an instance of an particular class.

Class is the logical entity, which means they really does't exist is the real world. And Objects are the physical entity, We see them by our naked eyes.

Let's take a simple example...

Have you seen an Animal?

No, no one has, because Animal(is a class: A logical entity).

So what I see on roads or in Zoo? Are not they Animal?

No, certainly not. They are Objects(A physical entity) of Animal class : like Horse, Lion, Elephant and so on.

So, An Objects belongs to a Class, But isn't a class. And a class cannot be an Object (Because Class is logical and Object is physical.)

Since the edits got approved and the terms Class and Object got used, the answers are "yes" and "yes":

"Can a Class be an Object in Java?"

Object o = String.class; // yes

"can an Object be a Class?"

boolean objectIsClass = o instanceof Class; // yes
Related