Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Java classes considered to be objects themselves? [duplicate]

Tags:

java

object

class

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.

like image 649
Torvaldy Avatar asked Aug 16 '13 16:08

Torvaldy


People also ask

What is duplicate object in Java?

lang. String objects a and b are duplicates when a != b && a. equals(b) . In other words, there are two (or more) separate strings with the same contents in the JVM memory.

Is a class considered an object in Java?

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

Are classes and objects the same in Java?

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. A Class is like an object constructor, or a "blueprint" for creating objects.

What is true about the object class?

The Object class is the superclass directly or indirectly of every other class in Java. The Object class itself doesn't have any superclass. In the absence of any other explicit superclass, every class is implicitly a subclass of Object.


4 Answers

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.

like image 126
nanofarad Avatar answered Oct 21 '22 00:10

nanofarad


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.

like image 20
Sotirios Delimanolis Avatar answered Oct 20 '22 22:10

Sotirios Delimanolis


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.

like image 2
jtravaglini Avatar answered Oct 20 '22 22:10

jtravaglini


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.

like image 1
Kishor Prakash Avatar answered Oct 21 '22 00:10

Kishor Prakash