About
An object:
- stores its state in fields
- and exposes its behaviour through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.
By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.
From a class, you can create several object (instance of a class) with different state but the same behaviours. A class provides the blueprint for objects; you create an object from a class. When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables.
A typical Java program creates many objects, which as you know, interact by invoking methods.
The phrase “instantiating a class” means the same thing as “creating an object.” When you create an object, you are creating an “instance” of a class, therefore “instantiating” a class.
Articles Related
Benefits
Bundling code into individual software objects provides a number of benefits, including:
- 1. Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
- 2. Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
- 3. Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
- 4. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
Data Type
Every object is either:
- or primitive
datatype
For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.Class.
Property
Identity
Id
The Java Machine has an internal id for each object created.
To verify that they are the same object use the == operator.
Debugger may also show an internal integer id such as 3128 or 3129 but this is an internal representation of their implementation. See ObjectReference uniqueID
Hash
Two objects may have different data but may be considered logically equals.
This happens with the help of a hash function.
There is two level of hash:
- a hash method on each object that you can overwrite.
Object.hashCode()
- The default hash function on the system level
System.identityHashCode(object)
Management
(Create|Instantiate) an object
Each of the following statements creates an object and assigns it to a variable:
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);
Each of these statements has three parts (discussed in detail below):
- 1. Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
- 2. Instantiation: The new keyword is a Java operator that creates the object.
- 3. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
The new operator
Instantiating a Class
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.
The phrase “instantiating a class” means the same thing as “creating an object.” When you create an object, you are creating an “instance” of a class, therefore “instantiating” a class.
The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate.
The return value
The reference returned by the new operator does not have to be assigned to a variable. It can also be used directly in an expression. For example:
int height = new Rectangle().height;
Class
Interfaces and abstract classes can never be instantiated. You must always instantiate a concrete class but you can assign the resulting object to an interface.
Example where:
- InterfaceFoo is an interface
- AbstractFoo is an abstract class
- ImplementationFoo is a concrete class
with this hierarchy
InterfaceFoo < AbstractFoo < ImplementationFoo
// Valid
InterfaceFoo foo= new ImplementationFoo ();
AbstractFoo foo = new ImplementationFoo ();
// Invalid
InterfaceFoo foo = new InterfaceFoo();
InterfaceFoo foo = new AbstractFoo();
AbstractFoo foo = new AbstractFoo();
Dynamic
For a class with a constructor without argument otherwise others reflection method must be used
Foo foo;
String fooClassName = getFooImplClass();
InterfaceFoo foo = (InterfaceFoo) Class.forName(fooClassName).newInstance();
Builder Pattern
Get
instanceof (Type)
Object o = new Integer("3");
if (o instanceof Integer)) {
System.out.println("This is an integer");
}
Drop an object
Some object-oriented languages require that you keep track of all the objects you create and that you explicitly destroy them when they are no longer needed. Managing memory explicitly is tedious and error-prone.
The Java platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you don't have to worry about destroying them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.
An object is drop:
- by the garbage collection when there are no more references to that object.
- explicitly by setting the variable to the special value null.
Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.
Equality
- The == operator verify that they are the same object. (Ie object id not the same value !)
- whereas the equals method verify that you have the same logical object (ie the same passport Id for instance)
toString
Helper: https://guava.dev/releases/19.0/api/docs/com/google/common/base/Objects.ToStringHelper.html
@Override
public String toString()
{
return toStringHelper(this)
.add("tableName", tableName)
.add("type", type)
.add("columns", columns)
.toString();
}
This (Object access)
this is a reference pointer to the object created by the class.
In an inner class, you can access the outer object with the qualified this - ie OuterClass.this from the InnerClass