View RSS Feed

My Java Tips

Object Pointer In Java

Rating: 1 votes, 1.00 average.
by , 11-08-2011 at 06:43 PM (3243 Views)
Pointer in C/C++ denotes to the variables saving the reference, In Java there is only call by reference. But here the pointer refers to an object which itself has another reference within it.

Java classes can have instance variables. If an instance variable is of class type, then it can hold instance of the class in it. This is called pointer to the object.

In some cases, we need an object that contains an instance variable that can refer to another object of the same type. Definition of the object's class is recursive in such cases. For example: there are many companies and each company is working under some other company. But one company say ‘JJ’ is totally independent and is not working in any other.

Implementing this logic in class named Company, we need an instance variable called " working_under " of type Company that will hold object of the company under whom current company is working. For ‘JJ’, the totally independent company, " working_under " will be referring to null.

Review the Company class. We have 5 companies and we have created 5 objects. Each is referring to its parent Company using instance variable of type Company called working_under. " JJ " being the parent company, has null in " working_under ". Now we want to check how many companies are there between a given company and the parent company. This can be done easily just by using if conditions and while loops.

Java Code:
public class Company {

String name;

    Company working_under;

public static void main(String[] args) {

Company JJ = new Company(); = "JJ";

        JJ.working_under = null;

Company AA = new Company(); = "AA";

        AA.working_under = JJ;

Company BB = new Company(); = "BB";

        BB.working_under = AA;

Company CC = new Company(); = "CC";

        CC.working_under = BB;

Company DD = new Company(); = "DD";

        DD.working_under = CC;

// checking DD's levels

        Company to_check = DD;

if ( to_check.working_under == null ) {


                    + " is the parent company!" );


        else {

            Company obj;

            obj = to_check.working_under;

            if ( obj.working_under == null) {


                        + " reports directly to the parent company." );


            else {

                int count = 0;

                while ( obj.working_under != null ) {


                    obj = obj.working_under;


                System.out.println( "There are " + count

                        + " companies between " +

                        + " and the parent company." );




As the while loop is executed, obj points in turn to the original Company, " to_check ", then to " to_check "'s supervisor, then to the parent of to_check's parent, and so on. The count variable is incremented each time to_check visits a new company. The loop ends when obj.working_under is null, which indicates that obj has reached the parent. At that point, count has counted the number of steps between " to_check " and the parent.

We can use the same concept to make linkedlists.

Submit "Object Pointer In Java" to Facebook Submit "Object Pointer In Java" to Digg Submit "Object Pointer In Java" to Submit "Object Pointer In Java" to StumbleUpon Submit "Object Pointer In Java" to Google

Tags: None Add / Edit Tags
Java SE