Static vs Non-static

R

Rajesh

Based on my understanding static members do not have access to non-
static members. In the below example static method 'name' accessing
non-static method creating reference. Seems to me it is not holding to
the theory? Pls explain.

Here is an example:
namespace StaticNonStaticMembers
{
class Program
{
static void Main(string[] args)
{
person p = new person();
System.Console.WriteLine(person.name());
System.Console.WriteLine(p.changename());

}
}

class person
{
public string changename()
{
return "changed";
}

public static string name()
{
person p = new person();
return p.changename();
}
}
}

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

Rajesh,

From a static method/property, you can not access the instance members
directly WITHOUT a reference. In your example, you have a reference to an
instance, and therefore, can call the instance methods on that instance.

Hope this helps.
 
P

Peter Duniho

Based on my understanding static members do not have access to non-
static members.

That's not true. A static member of a class can access non-static
members, as long as they specify an instance.
In the below example static method 'name' accessing
non-static method creating reference.

Yes, the static method "name()" does access the non-static method
"changename()" of the instance created. Of course, that doesn't prove
anything because the method is public and so *any* code can access that.

It would be more interesting for you to show code where a static method of
the class accesses a protected or private method or field of the same
class, since that's a scenario in which only the methods within the class
are allowed to access that member. You would find that also works, but at
least it is an interesting example.
Seems to me it is not holding to the theory? Pls explain.

What theory?

Pete
 
B

Bill Butler

Rajesh said:
Based on my understanding static members do not have access to non-
static members.

That definition is inacurate.
Try
static members do not have access to non static members of the same
class without first creating an instance of the object and accessing the
nonstatic members via the instance.

take a look at this:

using System;
class Foo
{
private int x;

public Foo(int x){this.x = x;}

static void Main(string[] args)
{
Foo foo1 = new Foo(5);
Foo foo2 = new Foo(10);
Console.WriteLine(foo1.x);
Console.WriteLine(foo2.x);
}
}

---------------------
prints
5
10

---------------------

Since there is one x for every instance of Foo, you obviously can't
access x without knowing which foo it lives in.

It is not that "they" are attempting to restrict what static method
can/cannot do. It simply does not make sense for a static method to
access a nonstatic member without an instance of the object.

Now you may argue "Hey, what if it is a nonstatic Method? why can't I
access that?"
For instance (added to prior example)
public void Method1()
{
Console.WriteLine("x:{0}",x);
}

and
public void Method2()
{
Console.WriteLine("Hello from the inside of Foo");
}

Method1 makes no sense without an instance of Foo because it needs to
know the value of x
Method2 on the other hand knows nothing of the internal state of
Foo...so why can't I call it????
Because you didn't make it static!!!

There is absolutely no reason for Method2 to NOT be static.

Hopefully, my rambling will help clear this up a bit

Bill
 
R

Rajesh

Based on my understanding static members do not have access to non-
static members.

That definition is inacurate.
Try
static members do not have access to non static members of the same
class without first creating an instance of the object and accessing the
nonstatic members via the instance.

take a look at this:

using System;
class Foo
{
private int x;

public Foo(int x){this.x = x;}

static void Main(string[] args)
{
Foo foo1 = new Foo(5);
Foo foo2 = new Foo(10);
Console.WriteLine(foo1.x);
Console.WriteLine(foo2.x);
}
}

---------------------
prints
5
10

---------------------

Since there is one x for every instance of Foo, you obviously can't
access x without knowing which foo it lives in.

It is not that "they" are attempting to restrict what static method
can/cannot do. It simply does not make sense for a static method to
access a nonstatic member without an instance of the object.

Now you may argue "Hey, what if it is a nonstatic Method? why can't I
access that?"
For instance (added to prior example)
public void Method1()
{
Console.WriteLine("x:{0}",x);
}

and
public void Method2()
{
Console.WriteLine("Hello from the inside of Foo");
}

Method1 makes no sense without an instance of Foo because it needs to
know the value of x
Method2 on the other hand knows nothing of the internal state of
Foo...so why can't I call it????
Because you didn't make it static!!!

There is absolutely no reason for Method2 to NOT be static.

Hopefully, my rambling will help clear this up a bit

Bill
Bill,

Since there is one x for every instance of Foo, you obviously can't
access x without knowing which foo it lives in.

It is not that "they" are attempting to restrict what static method
can/cannot do. It simply does not make sense for a static method to
access a nonstatic member without an instance of the object.

Your point makes perfect sense with above desc... It cleared the
confusion.

Appreciate your help !!!
 
R

Rajesh

That's not true. A static member of a class can access non-static
members, as long as they specify an instance.


Yes, the static method "name()" does access the non-static method
"changename()" of the instance created. Of course, that doesn't prove
anything because the method is public and so *any* code can access that.

It would be more interesting for you to show code where a static method of
the class accesses a protected or private method or field of the same
class, since that's a scenario in which only the methods within the class
are allowed to access that member. You would find that also works, but at
least it is an interesting example.


What theory?

Pete

Agree with you.

Theory meaning "static members do not have access to non-static
members."

But it cleared up .. thanks for your help.
 
C

Cor Ligthert [MVP]

Rajesh,

There is nothing that has access to a non-static member of a class, simple
because it does not exist as long as it is not instanced from the template
(the non static class). .

Only after that it is instanced as a part of your program, an object in this
case, you can use the members in that object.

A static member (which can be a part of your class) is nothing than a module
part of your program. It exist from the begin to the end and it will be
there from the start to the end of the program occupying the memory where it
is static in. Therefore those static parts will never be constructed by you.

Cor
 
C

Christof Nordiek

Rajesh said:
Based on my understanding static members do not have access to non-
static members.

static and non-static has nothing to do with accessibility.
The point is: non-static members can only be called in the context of an
instance. Inside that member this instance can be accessed by the keyword
"this".
Inside a non-static method, you can call any nonstatic member implicitly on
"this". In static members there is no "this", (because they don't run in the
context of an instance.) So this implicit reference to this is not possible.
But this is not a question of accessibility. You alwys name any instancs e
member explicitly on an instance.

Christof
 
R

Ravichandran J.V.

As long as you have an instance of the class, whose members you are
calling, you can access instance members from a static member. In your
example, you have a Person instance which is being used to call the
instance member.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

static 2
Error in Generic method: 'T' does not contain a definition for... 2
? 7
overriding static and non static method 6
error :( 4
what is base() in inheritance 2
inheritance problem 2
Enum Extentions 7

Top