By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Static vs Non-static

P: n/a
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.

Apr 13 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Thu, 12 Apr 2007 20:03:22 -0700, Rajesh <ra***************@gmail.com>
wrote:
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
Apr 13 '07 #2

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rajesh" <ra***************@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
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.

Apr 13 '07 #3

P: n/a
"Rajesh" <ra***************@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
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



Apr 13 '07 #4

P: n/a
On Apr 12, 8:37 pm, "Bill Butler" <qwe...@asdf.comwrote:
"Rajesh" <rajesh.godavar...@gmail.comwrote in message

news:11**********************@p77g2000hsh.googlegr oups.com...
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 !!!

Apr 13 '07 #5

P: n/a
On Apr 12, 8:32 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 12 Apr 2007 20:03:22 -0700, Rajesh <rajesh.godavar...@gmail.com>
wrote:
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
Agree with you.

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

But it cleared up .. thanks for your help.

Apr 13 '07 #6

P: n/a
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

"Rajesh" <ra***************@gmail.comschreef in bericht
news:11**********************@p77g2000hsh.googlegr oups.com...
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.

Apr 13 '07 #7

P: n/a
"Rajesh" <ra***************@gmail.comschrieb im Newsbeitrag
news:11**********************@p77g2000hsh.googlegr oups.com...
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
Apr 13 '07 #8

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Apr 18 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.