PCinvent.info

PCinvent Studio Template

Sep 29 2008
Object's assignment, storage & grabage Print E-mail
Monday, 29 September 2008


Read More Articles in: Technology>>>Programming

This Article is written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it


Many only know the syntax of the object oriented language, but never think of how it works and how the problem occures. Here is a simple tutorial I am going to write and explain.

Suppose we have a Class: Account.java ( Account.cs in the case of C# )......

 

 

 Account Class:

public class Account
{
    //  Instance Variables here
    private double accountBalance;

    //Constructors
    public Account (double openingaccountBalance)
    {
        accountBalance = openingaccountBalance;
    }

    public Account()
    {
        accountBalance = 0.0;
    }

    // Instance methods
    public void deposit(double money)
    {
        accountBalance += money;
    }

    public void withdraw(double money)
    {
        accountBalance -= money;
    }

    public double getaccountBalance()
    {
        return accountBalance;
    }

}//end of Account

Now, a class and its methods were done. If we have another class file called "operateAccount.cs" in the same directory of the Account.cs, to create a new object of Account, we will do:

Account account1 = new Account ( 100.00 );
Another object?
Account account2 = new Account ( 500.00 );

It is same as:

 Account account 1
account1 = new Account ( 100.00 );

Of course, both object can call the method to access:

account1.deposite ( 50.00 );
account2.withdraw ( 20.00 );

 

Ok, so far, the above code should be very well known. But what if we do:

Account account1;
or
account1 = new Account ( 100.00 )

Will it generate a compile error or run-time error? No, these syntax are correct.
so what does it mean?

 

Account account1

Account is a object data type. So this one is a object varible assigment.
Same concept can be referred from basic integer initalization:

int a;
At this point, the memory for the variable is allocated, but not value assigned yet. ( Some languages initalize value to 0 by Default) Similarly, this one does the same:
Account account1;
 

account1 =  new Account(100.00)

The new Account object will not stored in the account1 box. ( Common mistake)
Actually, the statment above will create a account1 varible which point to ( Reference to) the Account Object which contatin the value 100.00.

Garbage

Object will become a grabage where there is no varible referring to it.

For example, if we have the account1 object varible, when we say:

account2 = account1

Now the account2 will refer to the same object of the account1.

Also, if we do method of

account.withdraw ( 100.00 )

At the same time, bother account1 and account2 will have 100 withdrawn.

Knowing the object assignment, we are now going to talk about Grabage.

We have account1 and account2 variables:

account1 = new Account (100.00 )

account2 = new Account ( 900.00 )

Both refer to different objects.

now we do:

account1 = account2

Obviously, account 1 and account 2 are now pointing to (referring to) the Account object

Where there is 900.00 value stored. So we will never be able to use the Account Object whihc holds the data of 100.00 again.

Comments (0)Add Comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley

busy
Tags:  Technology Programming Irregular Object's assignment storage & grabage java C# object oriented statistic method instance method
Last Updated ( Tuesday, 30 September 2008 )
 
< Prev   Next >
Unlimited Phone Service $14.95
Site by PCinvent.com