1

i was just curious about it that why we have to use an object of a class in order to access the class member? i mean i know we can access the static members without creating an object. But why can't we access the other class members without using objects? what is the actual mechanism behind this?

For Example, Take a look at the following code:

public class Taxi
{
    public bool isInitialized;

    public Taxi()
    {
        isInitialized = true;
    }
}

class TestTaxi
{
    static void Main()
    {
        Taxi t = new Taxi();
        Console.WriteLine(t.isInitialized);
        Console.ReadKey();
    }
}

why we have to print the isInitialized variable using object and why can't we access it directoly?

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • Because member you're trying to access is not marked as `static`, which means it's an instance member by default. – MarcinJuraszek Mar 25 '14 at 20:05
  • 1
    What do you mean by directly? – RyanS Mar 25 '14 at 20:05
  • That's the "technical" answer (given by @MarcinJuraszek) ... the "concept" answer is that the method is a "behavior" or "trait" of `Taxi` objects. If you simply wrote `isInitialized()`, what would you be referring to? *What* "is initialized?" – TypeIA Mar 25 '14 at 20:06
  • The readings in this question should help you understand. http://stackoverflow.com/questions/2885385/what-is-the-difference-between-an-instance-and-an-object – Ray Cheng Mar 25 '14 at 20:13

6 Answers6

2

Because instance members are related to a specific instance. If you ask if a specific taxi is initialized, it makes sense, but it doesn't make sense to ask if taxis in general are initialized.

(isInitialized isn't the most obvious member to reason about; think about Color instead, if it makes more sense to you)

EDIT: I think that what's bothering you is that you can't access isInitialized directly in the Main method, even though it's in the same class. The reason is that Main is a static method, which means that it doesn't belong to a specific instance of the class. When you write isInitialized in an instance method, it's actually shorthand for this.isInitialized, but there is no this in a static method.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
2

Consider this code:

public class Taxi
{
    public bool isInitialized;

    public Taxi()
    {
        isInitialized = true;
    }
}

class TestTaxi
{
    static void Main()
    {
        Taxi t1 = new Taxi();
        Taxi t2 = new Taxi();
        t2.isInitialized = false;
        Console.WriteLine(t1.isInitialized);
        Console.WriteLine(t2.isInitialized);
        Console.ReadKey();
    }
}

In this case you can clearly see that the two instances are different. In this context it does not make sense to ask "What is the value of the isInitialized field in general?" since it can differ depending on the instance. What makes sense is to ask "What is the value of the isInitialized field for this object instance?"

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
1

Object Oriented Programming has the answer.

There are two main types of members, static and instance. Static members apply to all the class instances and instance members apply to single instance. What if you have multiple Taxi objects in your code. Each Taxi object will have its own state. Are you going to assume that each Taxi is initialized ?

Since your field is not marked as static, it is maintained per instance and that is why you require instance of class to access that field.

Habib
  • 219,104
  • 29
  • 407
  • 436
0

Because isInitialized is a property of a particular instance of an object.

Consider if you added this property to your class:

public string Color { get; set; }

Now would it make sense to ask what the color is of a specific actual taxi? Or could you ask about the class taxi? Since taxis can be any color, you need to specify which taxi you are talking about.

Taxi myTaxi = new Taxi();
myTaxi.Color = "Blue";
Taxi someOtherTaxi = new Taxi();
myTaxi.Color = "Yellow";

Similarly, your taxi is either initialized or not. It doesn't make sense to ask if all taxis are initialized, only if a particular one is.

Depending on the actual domain of what you are looking at, it might be argued that all your Taxi are part of a fleet belonging to one company and because of that, they should all be the same color. In that case, it might make sense to make it static:

public static string Color { get; set; }

And now you don't need an instance to access the color because it now a property shared by all instances of this class. Which makes repainting your taxis a lot easier:

Taxi.Color = "Blue";        // Now ALL my taxis are blue
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0

Suppose you have a class Bicycle

Class Bicycle
{
    public string SerialNumber {get; set;}
    ...
}

Does it make any sense to get the SerialNumber if you don't even have a Bicycle in the first place?

0

The reason that the object must be used to access the class member is because of scope.

The scope of a variable, sometimes referred to as accessibility of a variable, refers to where the variable can be read from and/or written to, and the variable's lifetime, or how long it stays in memory. - MSDN - Definition of Scope

The isInitialized variable is scoped to the Taxi class. As a result of Taxi being non static it must be instanstiated (have an instance created) in order to be used. Once created, there is a lot more that can be done than just access one member variable.

It can be passed around as a unique instance of a taxi, perhaps for a large set of taxis. It can be used to make some type inference. Perhaps there is a method which communicates with a database that is part of the taxi class. Members of taxi can also be made private or internal, and used only in calculation or without public access. All in all, having the instantiated object implies much more than accessing a single variable.

If isInitialized were scoped to or shared scope with TextTaxi then it could be used without the object reference.

The MSDN article linked above has a vary large and in depth explanation on this subject which I would advise you to read.

Travis J
  • 81,153
  • 41
  • 202
  • 273