OBJECT CLASS

Object

java object class

This section covers creating and using objects. You will learn how to instantiate an object, and, once instantiated, how to use the dot operator to access the object's instance variables and methods.
 
Class
This section shows you the anatomy of a class, and how to declare fields, methods, and constructors.
 
The Object Class
The Object(in the API reference documentation) class sits at the top of the class hierarchy tree in the Java development environment. Every class in the Java system is a descendant(direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class.
 
The equals Method
Use the equals() to compare two objects for equality. This method returns true, if the objects are equal, false otherwise. Note that equality does not mean that the objects are the same object. Consider this code that tests two Integers, one and another one, for equality:
 
1
2
3
Integer  one = new Integer(1), anotherOne = new Integer(1);
    if (one.equals(anotherOne))
    System.out.println("objects are  equal");

This code will display objects are equal even though one and another one reference two different, distinct objects. They are considered equal because they contain the same integer value. Your classes should override this method to provide an appropriate equality test. Your equals method should compare the contents of the objects to see if they are functionally equal and return true if they are.

The getclass() method is a final method (cannot be overridden) that returns a runtime representation of the class of this object. This method returns a class object. You can query the class object for a variety of information about the class, such as its name, its superclass, and the names of the interfaces that it implements. The following method gets and displays the class name of an object:

The getclass() Method

1
2
3
4
void  PrintClassName(Object obj)
    {
        System.out.println("The Object's class  is " + obj.getClass().getName());
    }

One handy use of the getclass method is to create a new instance of a class without knowing what the class is at compile time. This sample method creates a new instance of the same class as obj which can be any class that inherits from object (which means that it could be any class)

1
2
3
4
Object  createNewInstanceOf(Object obj)
    {
        return obj.getClass().newInstance();
    }
 
The toString() Method
Object's tostring() method returns a string representation of the object. You can use tostring() to display an object. For example, you could display a string representation of the current thread like this:
 
1
System.out.println(Thread.currentThread().toString());
 
The string representation for an object is entirely dependent on the object.
The string representation of an integer object is the integer value displayed as text.
The string representation of a thread object contains various attributes about the thread, such as its name and priority.
For example, the previous of code above display the following:
thread[main,5,main]
The tostring() method is very useful for debugging and it would behoove you to override this method in all your classes.
 
notify()
public final void notify()
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait() methods.
One handy use of the getClass method is to create a new instance of a class without knowing what the class is at compile time. This sample method creates a new instance of the same class as obj which can be any class that inherits from Object (which means that it could be any class):
 
1
2
3
4
Object  createNewInstanceOf(Object obj)
    {
        return obj.getClass().newInstance();
    }