When you create a class, you are creating a new data type. You can
use this type to declare objects of that type. However, obtaining objects of a class is a
two-step process. First, you must declare a variable of the class type. This variable does
not define an object. Instead, it is simply a variable that can refer to an object. Second,
you must acquire an actual, physical copy of the object and assign it to that variable. You
can do this using the new operator. The new operator dynamically allocates (that
is, allocates at run time) memory for an object and returns a reference to it. This
reference is, more or less, the address in memory of the object allocated by new.It has
this general form:


class-var = new classname( );

Here, class-var is a variable of the class type being created. The classname is the name of
the class that is being instantiated. The class name followed by parentheses specifies the
constructor for the class. A constructor defines what occurs when an object of a class
is created. Constructors are an important part of all classes and have many significant
attributes. Most real-world classes explicitly define their own constructors within their
class definition. However, if no explicit constructor is specified, then Java will automatically
supply a default constructor. This is the case with Box.A class creates
a new data type that can be used to create objects. That is, a class creates a logical
framework that defines the relationship between its members. When you declare an
object of a class, you are creating an instance of that class. Thus, a class is a logical
construct. An object has physical reality.


0 comments to "OBJECTS"

Post a Comment

Powered by Blogger.