Object Behavior

You already learned about object in previous tutorial. But you had many queries regarding objects – Why Objects? What’s its use?

In previous tutorials you wrote codes in a class with main method.  Ever wondered if you can create methods(like bark(), hungry()) in a class other than the main method class. What we are trying to say is what if there are two classes

  1. one Tester{ } with main() method (you already know that JVM only runs code in main() method only).
  2. other class Dog{ } with the methods that we want to create (bark(), hungry(), sleep()).

 

JavaObjectsCodeEasy

#CodeEasyJavaObjects

 

But how to use it? How to run the code or methods in Dog{ } class?

Somehow, we need to access these methods [ e.g. sleep(), bark(), hungry() ]. To access these methods, we need to create an object.  Object is created by calling its class instance in the main() method. Think of instance as another way of saying object. 

Note: You can say obj1 is an instance (example) of Dog { } class.

The DOT operator (.) gives you access to an object’s state and behavior (i.e. instance variables and methods respectively.)


Creating Object:

Objects are created using new keyword as mentioned below in the syntax.

Syntax:     ClassName objectName = new ClassName();

 E.g.           Dog  obj1 = new  Dog();

Accessing Methods:

Methods are accessed by using dot(.) operator with objects as show in the below syntax.

Syntax:           objectName.methodName(parameters);

E.g.                   obj1.bark();

#JavaObjects #JavaCodeEasyObjects #ObjectsWorkingJava #CodeEasyJavaTutorials

Try  the above code and observe the output.


 

But what exactly is happening here? How does your code work?

When you compile and run your main class (Tester{}) – JVM searches the main method and executes the code inside it. As soon as JVM finds an object (obj1) in main method it calls its class (Dog {}) whose object is created and starts accessing its methods (obj1.bark()).

Remember you learnt that “A class is a blueprint for an object”.

So, what does it takes to create and use an object?

  • You need two (or more) classes.
  • One class for the type of object – (Dog{} class).
  • Another class to test your new class- (Tester{} class).
  • Tester class has the main method, and in that main method you create and access objects of your new class type using dot operator.

Now you understood that tester class is only responsible for trying out the methods and variables of your new object class type.

 

The fun part is you can construct as many objects as you want.
#CodeEasyjavaTutorials

So, what is the advantage of making objects? You can see that, you don’t need to create a separate class and methods for each separate dog. Instead you made a single blueprint of a dog class with methods and variables. You can make number of objects for each dog type as shown above.


In previous tutorial you learnt that an object has a state(variables) and a behavior(methods).

Above you learnt how to access methods. Here you will learn how to access variables. Approach is very much similar.

  • First declare your variables in Dog{} class.#JavaObjects #CodeEasyJavaTutorials #JavaObjectCodeEasy #JavaTutorialsObjectsCodeEasy
  • Next construct its object in main class and access your variables using dot(.) operator.

#JavaObjects #CodeEasyJavaTutorials #JavaObjectCodeEasy #JavaTutorialsObjectsCodeEasy

You may store your values in a new variable in the main class and then print it out.

E.g.  int a = obj1.age ;

System.out.println(a);

You will get the same output.

Try out the above code and observe the output.


 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s