All About Class Object in Java

0
134
Spread the love
  • 24
  • 34
  • 14
  • 23
  •  
  •  
  •  
  • 12
  •  
  •  
    107
    Shares

Class Object in Java
Class Object in Java
 
 
 
 
 
 
 
 
 
 
 
The Object class is the root of all classes available in Java. So it is the parent in the hierarchy. Every class has Object as a super class(parent).
All objects implements methods of this class. The structure of original Object class is as follows:
public Class java.lang.Object{
//Conctructor
public Object();
// creates a new instance of the Class Object and allocates memory for it.
/Methods:
protected Object clone();
protected void finalize();
public boolean equals(Object obj);
public final Class getClass();
public int hasCode();
public final void notify();
public final void notifyAll();
public String toString();
public final void wait();
public final void wait(long timeOut);
public final void wait(long timeOut,int nanos);

}
 
Remember, declaring a variable to hold an object does not create the object. The  variable just holds the reference to the object.

How to create Objects

To create an object , we need to use the new keyword followed by the object’s class or type with optional argument lists in the parenthesis. These arguments are passed to the constructor of the class.

ComplexNumber c=new ComplexNumber(3.0,1.414);
Other ways to create objects
  • We can create a String object by enclosing the characters in double quote. like-
    
    String myStr="I love Java";
    // This is the shortcut for String Objects only
    
  • By calling newInstance() method of a class object. We use them generally in dynamic loading of classes.
  • Objects can also be created by deserializing them. That is recreating an object that had its state saved via serialization.

How to Access the Objects?

Java objects are always accessed by its references. Java internally performs any necessary derefencing.  A dot (.) operator provides a mechanism to gain access to the object.

double magnitude=e.magnitude();
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 34
  • 14
  • 23
  •  
  •  
  •  
  • 12
  •  
  •  
    107
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here