473,386 Members | 1,602 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,386 software developers and data experts.

Re: Static vs Instance Method Question

On Aug 11, 5:11*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
As Arne says, it's mainly a matter of an arbitrary choice in the language*
to help make sure it's obvious what type of method is being called. *Not *
that IMHO it's so important that one knows a method is static vs instance, *
but rather than one may expect different behaviors (especially with *
respect to polymorphism) when calling a method via an instance rather than *
via a type name.

By requiring the static calls to be made using the type name instead of an *
instance, it's a lot more clear in code what's going on.
Exactly. My favourite example is Thread.Sleep. Suppose this code were
valid (its equivalent in Java is, for example):

ThreadStart ts = SomeMethodToRun;
Thread thread = new Thread(ts);
thread.Start();
thread.Sleep(500);

What does it *look* like that call to Sleep is doing? To me, it looks
like it's acting on the newly created thread. By forcing you to write
Thread.Sleep, C# makes it obvious that you can't call Sleep on an
arbitrary thread - the operation only makes sense for the current
thread, which can be determined statically.

Jon
Aug 11 '08 #1
4 1898
On Mon, 11 Aug 2008 23:20:17 -0700, Arcticool <ar*******@hotmail.com>
wrote:
[...]
public class Tester
{
int i = 32; //compile error, must be static, makes sense...
static void Main()
{
static int j = 40; //for some reason throws compile error.
Console.WriteLine(i);
Console.WriteLine(j);
}
}

Is 'j' implicitly static by virtue of being inside a static method? Or
is static just not valid inside a method?
The latter. C# just doesn't have static locals (and the variable "j" is
definitely not implicitly static, at least not in the sense that a static
local would be in C++).

I'm confused by your comment about the compile error for the instance
member field "i" though. You would get an error about the field needing
to be static if the class itself was static, but given the code you
posted, I think that line should compile fine. What error are you getting
there, exactly? Do you get it with exactly the code you posted?

Pete
Aug 12 '08 #2
Peter Duniho wrote:
On Mon, 11 Aug 2008 23:20:17 -0700, Arcticool <ar*******@hotmail.com>
wrote:
>[...]
public class Tester
{
int i = 32; //compile error, must be static, makes sense...
static void Main()
{
static int j = 40; //for some reason throws compile error.
Console.WriteLine(i);
Console.WriteLine(j);
}
}

Is 'j' implicitly static by virtue of being inside a static method? Or
is static just not valid inside a method?

The latter. C# just doesn't have static locals (and the variable "j" is
definitely not implicitly static, at least not in the sense that a
static local would be in C++).

I'm confused by your comment about the compile error for the instance
member field "i" though. You would get an error about the field needing
to be static if the class itself was static, but given the code you
posted, I think that line should compile fine. What error are you
getting there, exactly? Do you get it with exactly the code you posted?

Pete
Oh that's probably my bad I was playing with some constructor stuff and
tried to simplify the code for the post here. Anyway here is the
original code:

public class Tester
{
int j = 32;
static void Main()
{
Console.WriteLine(j); //j here is underlined in blue.

System.DateTime currentTime = System.DateTime.Now;
Time t = new Time( currentTime );
t.DisplayCurrentTime();

Time t2 = new Time( 2007, 9, 18, 11, 45 );
t2.DisplayCurrentTime();

Time t3 = new Time();
t3.DisplayCurrentTime();
}
}

//An object reference is required for the non-static field, method, or
property 'Initializer.Tester.j'
BTW, I'm using Visual C# Express edition.

Jack
Aug 12 '08 #3
On Mon, 11 Aug 2008 23:54:33 -0700, Arcticool <ar*******@hotmail.com>
wrote:
[...]
//An object reference is required for the non-static field, method, or
property 'Initializer.Tester.j'
Okay, well that's a completely different line of code and a different
error. It's not complaining that "j" has to be static per se, though
that's certainly one way to fix it. It's just pointing out to you that to
refer to an instance member, you need to have an instance, which in a
static method you don't.

Pete
Aug 12 '08 #4
Peter Duniho wrote:
On Mon, 11 Aug 2008 23:54:33 -0700, Arcticool <ar*******@hotmail.com>
wrote:
>[...]
//An object reference is required for the non-static field, method, or
property 'Initializer.Tester.j'

Okay, well that's a completely different line of code and a different
error. It's not complaining that "j" has to be static per se, though
that's certainly one way to fix it. It's just pointing out to you that
to refer to an instance member, you need to have an instance, which in a
static method you don't.

Pete
Hmm, for some reason my last response didn't post. Anyway, I see what
you're saying. I can make j static or I can create an instance of Tester
and then call Tester.j.

Thanks to everyone for the replies,

Jack
Aug 13 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
15
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
0
by: Joe Strout | last post by:
Hi Luis, A static variable IS encapsulation. Encapsulation happens at many levels: module, class, instance, and (in languages that support it) method. A static local variable is simply the...
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:
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.