Instance variable and instance of the variable.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 3821 Views - Last Post: 19 October 2009 - 09:57 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Instance variable and instance of the variable.

Posted 15 October 2009 - 08:03 PM

The book, according to the page where instance variables are mentioned, "When each object of a class maintains its own copy of attribute, the field that represents the attribute is also known as an instance variable.

Different sources have different explanations, and quite confused and I even asked this question before, and somebody already answered it And I think I understand what fields and instance variables are. But according to the explanation the book provides, I just wanna confirm to see if I really understand what it means.

Doesn't it mean that when we first create the variable inside class, that's called field.

public class Gradebook
{
	private string courseName;


but after the object has been created, it is also known as an instance variable?

Gradebook object = new Gradebook();


But when the books mentions that "Each object(instance) of the class has a separate instance of the variable." I am quite confused about it. Does it mean that the instance object has another instance(object) from the variable? What is the different between instance variable and the instance of the variable? Are they the same? I don't really get it.

The book is written by P.J Deitel and H.M Deitel named visual C# how to program 2008.

This post has been edited by kenryuakuma: 15 October 2009 - 08:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Instance variable and instance of the variable.

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Instance variable and instance of the variable.

Posted 15 October 2009 - 09:04 PM

What the book is trying to say that is if you have two objects of a class, object1 and object2. The fields of object1 and object2 are separate entities. That object1.field1 and object2.field1 do not have the same value and point to different spaces in memory.

There are two exceptions to that rule though. If a field is static it is shared by all objects belonging to the class. The second is that since objects are reference types if you assign object2 to object1 they both point to the same space in memory.
Was This Post Helpful? 0
  • +
  • -

#3 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 07:00 AM

Got it thanks! But I thought we only use instances, that's, objects, to refer to objects...But instance of the variable doesn't sound right or make any sense to me.

If it says instance of the variable, that sounds or means to me as if it were the variable's instance, that means, the variable contains an object because instance = object.
Was This Post Helpful? 0
  • +
  • -

#4 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 08:54 AM

You're making this more difficult than it needs to be.

As SixOfEleven stated, when the author said "Each object(instance) of the class has a separate instance of the variable.", he is saying is that there can be multiple instances of a single class. Each instance of the class then has it's very own instance of the variables that the class contains. Each instance of the class' variables contains it's own value that points to a unique space in memory (with the exception of static and reference variables).

This post has been edited by lesPaul456: 16 October 2009 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 09:13 AM

Well...I know it is a bit too much but is it possible to list some codes with explanations? That would be a great help.
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 09:19 AM

class MyClass {
	public int myInstanceVariable
}


myInstanceVariable is an instance variable. It doesn't exist anywhere, since there is no object created for it to exist in at this time.

 MyClass a = new MyClass();


Now, in a we have an instance of the variable myInstanceVariable. It does exist somewhere as we have created the object that contains it.
Was This Post Helpful? 0
  • +
  • -

#7 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 10:10 AM

To add to what Momerath posted, here's an example...

Here is a class that contains only one variable (an instance variable):

class ExampleClass
{
    public string MyString;
}



Now, in the main method of a sample application, I create two instances of that class:

class Program
{
    static void Main()
    {
        ExampleClass exampleClass1;
        ExampleClass exampleClass2;
    }
}



So, there is now two instances of the ExampleClass, exampleClass1 and exampleClass2. Each instance now has its own instance of the MyString variable. Each instance of the MyString variable can store its own value, and points to its on space in memory:

exampleClass1.MyString = "Value of exampleClass1.MyString";
exampleClass2.MyString = "Value of exampleClass2.MyString";


This post has been edited by lesPaul456: 16 October 2009 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#8 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 11:41 AM

Yes...as I expected and is much clearer and thanks for all you guys putting an effort in helping me understanding this term. Now annoying question, which you guys might find, is the TERM instance. Does instance alway refer to an object? Is an object an instance? because example ExampleClass exampleClass1; is not an object and how come it is called instance? I thought instance is only used to refer to object.
Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 11:45 AM

When you actually create an object, that is an instance of that object.
Was This Post Helpful? 0
  • +
  • -

#10 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 12:40 PM

View Postkenryuakuma, on 16 Oct, 2009 - 12:41 PM, said:

Yes...as I expected and is much clearer and thanks for all you guys putting an effort in helping me understanding this term. Now annoying question, which you guys might find, is the TERM instance. Does instance alway refer to an object? Is an object an instance? because example ExampleClass exampleClass1; is not an object and how come it is called instance? I thought instance is only used to refer to object.


Actually, exampleClass1 is an object.

An object is an instance. Maybe this article will help clear things up: http://msdn.microsof...y/ms173110.aspx.

This post has been edited by lesPaul456: 16 October 2009 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#11 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 03:17 PM

After reading over and over again about Momerath's post and the codes of lesPaul546, I think I understand what instance is. But the last question would be the object, exampleClass1;

don't we instantiate an object like this

Myclass object = new Myclass; while Myclass is the class identifier?
ExampleClass exampleClass1; ??????
Was This Post Helpful? 0
  • +
  • -

#12 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 04:13 PM

Sorry, I forgot to post the code to intantiate the instances of ExampleClass. :stupid:

Yes, before the class is instantiated, it is not an instance, since it has not yet been created, just declared.

So...
// Class declared, but not created.
ExampleClass exampleClass1;

// The exampleClass1 is now instantiated, and is an instance of ExampleClass
exampleClass1 = new ExampleClass();



Sorry again!

This post has been edited by lesPaul456: 16 October 2009 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

#13 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 06:09 PM

Just wanna clarify the English Grammar used in the context.

When referring to "an instance of the variable myInstanceVariable", does it mean that the instance belong to the myInstanceVariable, or to be concise, MyInstanceVariable's instance? That means the instance variable MyInstanceVariable contains the instance or possesses the instance(object).

because sth of sth = sth's sth. The tail of the cat = The cat's tail.

Or "an instance of the variable myInstanceVariable", means an instance(object) contains its own copies of the instance variables? I find the second question make more sense to me.

If it means the instance contains its own copies of the instance variables, that means improper grammar is used in the book written by P.J Deitel and H.M Deitel.

it should have been written "an instance with separate instance variable" rather than an instance of separate variables.

I read you guys' explanations with codes, which is real clear that how instances and instance variables are used, as mentioned earlier in my post. But an instance of the variable myInstanceVariable, gives me a mix feeling that the instance variable contains an instance(object) according to the English Grammar usage.
Was This Post Helpful? 0
  • +
  • -

#14 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 08:23 PM

You're thinking about this too much.

The instance does belong to the variable, but not how you think. You can think of an object or instance as the space in memory that the class takes up. There can be several instances of a class. The variables in a class are also classes (or sometimes structs). These can also contain their own instances (spaces in memory).

I hope this helps clear things up. I suggest that you read the C# programming reference on the MSDN site for more in depth information classes, structs, variables and objects/instances.
Was This Post Helpful? 0
  • +
  • -

#15 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Instance variable and instance of the variable.

Posted 16 October 2009 - 08:26 PM

To put it plainly, since C# is a strongly typed object-oriented language. You can think of all variables as objects. Every variable in C# can be created using the new keyword. For example, you can create an int variable as follows:

int myInt = new int();



That is perfectly legal in C# because C# treats everything as an object.

When you see the word "instance" that refers to a specific object of a class. myInt above refers to a specific object of int or instance. In the above program exampleClass1 and exmapleClass2 refer to specific instances of the class. An object is what is created by classes. Classes are just templates, or some prefer factories, for creating objects. Once you create an object the class the variable that refers to that object is an instance of the class. Does that clear things up?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2