4

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
  • 1
    A class is a theoretical concept. `class` is a keyword denoting the definition of a class. A `Class>` is a class representing whatever was defined by `class Whatever { ... }`. This will get very confusing if you do not separate between the keyword `class`, the concept of a class and the class `Class` and instances of classes and of the class `Class`. – luk2302 Nov 04 '17 at 12:02
  • You are asking in context of java or **Object-oriented programming (OOP)**? Because OOP is a paradigm which is implemented in many ways : Java, c#, C++ and many more. – Mehraj Malik Nov 04 '17 at 12:06
  • 2
    Note that the answer will depend entirely on the language being used. *In Java*, the accepted answer is correct. In some other languages, classes *are object* (and vice versa). – Konrad Rudolph Nov 04 '17 at 13:25
  • Clear downvote after the approved edit (by @MehrajMalik), now the question makes no sense. Now the terms of my first comment are all mixed with each other. In particular the last question is a clear **yes** since `String.getClass()` is an object of the class `Class`. – luk2302 Nov 04 '17 at 13:57
  • @maryanne Konrad Rudolph raises an important point -- you are intuitively right, it just isn't the case in Java, at least not conceptually (things get different through reflection). But, e.g., in Scala, "static members" are members of a [companion object](http://scala-lang.org/files/archive/spec/2.12/05-classes-and-objects.html#object-definitions) (which are not "class objects", but singletons with the same name); ... – phipsgabler Nov 04 '17 at 14:15
  • ... in Python, classes are objects of [metaclasses](https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python) and you can implement static members on them; in Julia, a dynamic language, [types are values, too](https://docs.julialang.org/en/stable/manual/types/#Operations-on-Types-1). – phipsgabler Nov 04 '17 at 14:15
  • @KonradRudolph Indeed. In Ruby : `Class.is_a?(Object) && Object.is_a?(Class)` is `true` – Eric Duminil Dec 02 '17 at 14:43

6 Answers6

3

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.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
2

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!

Sweeper
  • 213,210
  • 22
  • 193
  • 313
2

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

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

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.

IEE1394
  • 1,181
  • 13
  • 33
0

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.)

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
  • What about `String.getClass()`, what does that return? A `Class>` object which is a `Class`, not a *class* though. – luk2302 Nov 04 '17 at 14:00
  • As I mentioned, An object belongs to a Class that does't mean Object is a Class. getClass returns the belonging class that does not make the Object **class** – Mehraj Malik Nov 04 '17 at 14:02
  • `String.getClass()` **is** a `Class`, an instance of a class, and an object. – luk2302 Nov 04 '17 at 14:03
  • I am not talking about implementation, I am talking about **OOP**, please correct me if wrong. – Mehraj Malik Nov 04 '17 at 14:04
  • then you should not put Object and class in backticks (in the question), which makes them code. And since we are talking explicitly about java `Object` and `Class` are actual classes and can have actual instances. – luk2302 Nov 04 '17 at 14:11
0

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
luk2302
  • 55,258
  • 23
  • 97
  • 137