Object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Object

  1. #1
    Join Date
    Jul 2005
    Posts
    20

    Object

    An address is created but the memory is not created. e.g.
    Student m;

    An object is created when a constructor is invoked. e.g.
    Student m = new Student();
    This means that the address is created and the memory is created.

    Since we know that an object is created when a contructor is invoked, therefore can I say that for:

    Student m;

    m is a variable of the type Student?

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Yes.

    If you use the term "variable" to be a "name of a memory location where you can access specific data of a particular datatype" then "m" is the memory location where you can access the store of information which is a particular instance of an Abstract Data Type "Student".

  3. #3
    Join Date
    Jul 2005
    Posts
    20
    So m is a reference to the memory location in which the student object is being stored.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    [I forgot to add to my last post that, in OOP, we would more correctly call m an "instance" of the ADT Student but you certainly can use the word "variable" to talk about "m".]

    In programming the word "reference" is loaded with all sorts of meaning other than the context of "m is the name we use to refer to the memory location". I would disagree with your statement as you have stated, but agree with it in the "to refer to" meaning of "reference".

  5. #5
    Join Date
    Jul 2005
    Posts
    20
    So m holds a null reference or a reference to an instance of a class.

    e.g.

    Student m; //Address is created but memory is not created.
    Student m = new Student(); //Address and memory is created.

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Hmmm ... there's that "reference" word. Yes, since you are talking about an object, then the name for that object is a "reference variable".

    Yes ... if you write "Student m;", a memory location, named "m", is created on the stack and it holds the special reserved Java constant "null". Nothing is set aside, marked, or created on the heap.

    But if you write "Student m = new Student();", an instance of the Student class is created on the heap and its starting memory location is the content of the memory location named "m" created on the stack. So, when you call "m", the runtime engine will read that memory which contains the address of that particular instance of Student and it therefore is "referred" to that instance of Student.

  7. #7
    Join Date
    Jul 2005
    Posts
    20
    I declare a reference variable called "m" as below:

    Student m; //Address is created but memory is not created.

    Now the memory location called "m" is created in the stack memory. The stack memory holds the addresses of the objects.

    When I create an object of a Student class as below:

    Student m = new Student(); //Address and memory is created.

    The object created is stored in a heap memory. Since "m" holds the reference to an instance of a class called Student, therefore I can use the reference variable called "m" to access the values of the Student object stored in the heap memory.
    Last edited by solomon_13000; 06-13-2009 at 02:02 PM.

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    "Student m; //Address is created but memory is not created." Your comment is not a correct statement. Nothing (including no address) is created on the heap. As a result of this Declaration the run time machine creates a variable space but no corresponding object.

    One of (but not the only) the uses of "stack" memory in currently used memory models is to hold an "activation record" created for the program which (among other things) sets aside a slice of the stack memory to hold "variables" of the program. In a Java program, the "variables" are named and must be typed, and the space set aside within the activation record for the program for a value will either hold a scalar value (if the variable is typed of one of the primitive datatypes) or the memory address of the location of an object of any of the other datatypes in the heap memory (a reference value).

    "therefore I can use the reference variable called "m" to access the values of the Student object stored in the heap memory." Remember that "m" will give you access to that instance of a Student. You must use methods provided by your Student class to access the data members of the instance (if you have designed your class well). The "." in m.getName() or m.name is an invoke or call operator, which gives you access to public data or public methods of the instance of Student referred to as "m".
    Last edited by nspils; 06-13-2009 at 04:02 PM.

  9. #9
    Join Date
    Jul 2005
    Posts
    20
    Student m;

    But m is a reference type variable right. So m can be called an address right since m is referred to as a memory location.
    Last edited by solomon_13000; 06-14-2009 at 12:49 AM.

  10. #10
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I think you are trying too hard with this, as if you are trying to fit some kind of pre-set phrasing.

    No, "m" is a variable. It is the artificial name of a location (a section of the memory, with a series of addresses) within the memory available to the CPU. The value stored in the section of memory which we access by the name "m" is an address (when a Student object has been instantiated) in another part of the memory where that particular instance of Student is being stored. Since it holds the information that tells the run time machine where to look for the instance of Student we are interested in when it encounters the variable "m", we call it a reference variable.

Similar Threads

  1. How Vietnam Can Still Be Won
    By Steven Hughes in forum Architecture and Design
    Replies: 0
    Last Post: 02-19-2008, 08:07 AM
  2. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 01:15 PM
  3. Even C++ had this much right...
    By Derek Mooney in forum .NET
    Replies: 94
    Last Post: 10-29-2001, 09:44 PM
  4. Validating XML
    By Jaco de Villiers in forum XML
    Replies: 1
    Last Post: 06-01-2001, 06:50 PM
  5. Re: COM object Password Security
    By Tom Shreve in forum Enterprise
    Replies: 0
    Last Post: 04-07-2000, 09:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center