Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Java classes objects?

Tags:

java

oop

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?

like image 231
itdoesntwork Avatar asked Aug 16 '12 15:08

itdoesntwork


People also ask

Are classes objects?

A class is a template for objects. A class defines object properties including a valid range of values, and a default value. A class also describes object behavior. An object is a member or an "instance" of a class.

Are classes and objects the same?

A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.

What is an objects in Java?

A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created at runtime from templates, which are also known as classes.

What is the difference between classes and objects in Java?

Answer: A class is a template used for the creation of objects. An object is an instance of a class. While a class is a logical entity, an object is a physical entity. Each object has a state in which all the member variables have specific values.


4 Answers

A Java class is not an object.

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

like image 103
SLaks Avatar answered Oct 21 '22 08:10

SLaks


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.

like image 25
biziclop Avatar answered Oct 21 '22 10:10

biziclop


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.

like image 33
Babak Naffas Avatar answered Oct 21 '22 08:10

Babak Naffas


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.

like image 37
Fabrizio Fernandez Avatar answered Oct 21 '22 09:10

Fabrizio Fernandez