سؤال

We all know state of an Object is value of it's attributes (instance variables), but if class doesn't has any attribute (no inherited attributes), what would be the state of an Object of such class.

هل كانت مفيدة؟

المحلول 2

There is no such thing as a Java class without a parent class. The default parent would be used, e.g. java.lang.Object.

At a minimum every instance of a class has two attributes: a reference address and a Class type. Note, not every class can be instantiated. There is also some space used in the ClassLoader and any String(s) may (or may not) be interned. This actual implementation might vary slightly on the specific version of the JDK and run-time platform, and additional optimizations can be added by the JIT. However, as a Java developer you are not responsible for this memory management and I would be wary of premature optimization.

نصائح أخرى

There is a word for such objects - stateless.

first thing

any class we write in java will extend Object class by default if there is no extends written by the developer.

so each and every class will definitely have a parent with no doubt atleast Object class.

second

if you dont put any attributes in your class , obviously it will get all the instance variables except private gets inherited to your class.

so it will have atleast object state but it will not serve any purpose

An object with no data members and links to other objects is a stateless object and in this form can hardly be of any use.

This kind of classes can nevertheless be usefull, because of its methods. It can be...

  • a base for a further inheritance. It declares/defines some methods, that could be inherited by derived classes. This class will probably be an abstract class, having no objects at all (although not a condition)
  • a service class. It can define some methods, which in nature do not belong to concrete objects but are used by other objects. Like some all-purpose mathematical operations, a service that returns a current time or similar. These methods can be static, so again no instances are needed.

We call those object stateless. As the name suggests, they have no state.

Referring to other answers/comments, even though every Java object implicitly extends Object, mind that Object has no fields. So even though every object has a runtime address and class attributes, for all practical purposes you can still consider some objects stateless.

Next, it is definitely not true that stateless objects serve no purpose! You can use stateless object for:

1) Grouping functions with similar functionality, similar to java.lang.Math, which groups mathematical functions. 2) Passing functionality as a parameter, e.g. Comparator<T> can be used to sort objects that do not implement Comparable<T>, and it definitely needs no state.

Stateless objects are somehow similar to immutable objects: their state can never be changed and therefore they are always thread-safe.

You may also want to see JEE Stateless Session Beans which differentiate between a converstional state and an instance state.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top