473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Static vs Non-static

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
8 2032
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
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
"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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.