Objects in Java
creation of Objects using the “new“ keyword.
In general
sense an Object is any physical or conceptual thing that we find around us. If
we look around we will see many examples of real-world objects: your classmate,
your table, your television etc.
The above
three objects mentioned share similar characteristics.
They all have identity, state and behavior.
For example
your dog has state (name, color, breed) and behavior (barking, fetching and
wagging tail).
Identity is
the name given to an object, like name given to a dog.
So when you
create a class and create an object of that class the object will be having
access to the members or fields of that class and we access the members of that
class by using the object.
Imagine the
Object of a class as an agent of that
class. Whenever you need to access the members of that class you need to do it through
the agent (in our case the Object) we created for that class.
Consider a
sample program below.
Carefully
note that I have creates two different classes; SampleClass and SampleObject.
I have
declared the values of a,b and c in the class SampleClass.
Now suppose I
need to access these members from other class so I will create a class names
SampleObject.
The next
thing I do is creating the object of that class whose members I need to access.
In our case it is SampleClass and the members that I need to access are a,b,c.
Syntax
for
Here we
shall learn how to create an object of a particular class.
The general
syntax is given below.
By using the
above syntax mentioned we can create the object for class SampleClass as below.
SampleClass obj = new SampleClass();
The above
statement will define an object named “obj” and by using this object we access the members of class SampleClass.
In nest post we shall discuss the role of "new" keyword and some examples of object creation.
Bye! Keep visiting.




