70. How to use dot(.) operator

1. Launch Eclipse IDE and ensure that '' Class file contains three instance variables as shown below:

2. Now lets access the instance variables  in the above Box class, by creating an object for the above Box Class in another class called 'DotOperatorDemo' by following the next steps.

3. Create a new class named 'DotOperatorDemo' under 'Third Project' as shown below:

4. Create an object for the Box class in DotOperatorDemo class as shown below

5. Type box1 and type '.' as shown below and observe that instance variables declared in Box class will be displayed as shown below:

6. Select any one of the displayed instance variable say depth and the selected instance variable will be added to your code as shown below:

7. Now assign a value to the inserted variable as shown below:

8. Now print the value of  box1.depth as shown below:

9. Save the DotOperatorDemo class and Run it As -> Java application as shown below:

10. Observe that the value 100.0 got printed in console as shown below:

Download this program:

Click here to download the program which contains the DotOperatorDemo class (After download, you can import this project to Eclipse IDE installed on your machine)

Please comment below to feedback or ask questions.

As explained in this post, we've used dot (.) operator to access instance variables of a class by creating object to that class. In the same way we can access methods of the class by creating an object to it.  How to access methods of a class by creating object for the class will be explained after Explaining the concept of Methods in next post.


Unknown said...

Hi Arun,
Here in the above BOX and DotOperatorDemo classes are created in the same project but different packages, in this case not possible to execute the above what to do?

Arun Motoori said...
This comment has been removed by the author.
Arun Motoori said...


You have to import the Classes which are outside of your package.

Arun Motoori

Unknown said...