Log in

View Full Version : C# and Java STATIC question...


Typhoon
07-11-2005, 06:01 AM
The static modifier in a program, for example:

class Demo
{
public static void Main()
{
}
}

prevents more than 1 instance of a particular program from executing?

JonMisurda
07-11-2005, 06:10 AM
*Whips out intro to java teaching hat, places on*

static as a modifier to a variable or method indicates that the variable/method is not associated with a particular instance of a class, but rather is shared among all instances (or among no instances as the case may be.)

Now practically how to intepret this depends on the approach you're taking to the program and your background knowledge.

To answer your question directly, it has nothing to do with how many instances of your program there are.

Main is the entry point for the program, and as such, nothing in the program exists before Main() is started. Because there are no objects or anything to send messages to (call methods on,) all the methods and data you use must be independent of any objects. This is "static" as we defined before, and thus main will always be static.

The class I teach does not focus on proper object oriented programming, so I tell my students that its just syntax, memorize it. If you're comfortable with objects there are better ways to explain this. Let me know if that makes any sense and if not, what your background in programming is, and maybe I can help you out more.

Jon

Typhoon
07-11-2005, 06:25 AM
Ohhh ok. Thanks, I get it. Yea, actually that was the way I was looking at it long before...somehow I adopted some sort of wierd curiosity about it and felt like asking that question. So you teach and you are new here? Are you new w/PPCs? I have had mine for, at the most, 4 years.

JonMisurda
07-11-2005, 06:41 AM
Ohhh ok. Thanks, I get it. Yea, actually that was the way I was looking at it long before...somehow I adopted some sort of wierd curiosity about it and felt like asking that question. So you teach and you are new here? Are you new w/PPCs? I have had mine for, at the most, 4 years.

I'm hardly new, but these two statistics don't really mesh:

Joined: 11 Oct 2002
Posts: 21

I'm not much of a poster 8) .

I've had a Jornada 548, Axim x5, and now an Axim x50v, so however many years that is... 4 or 5 maybe.

I teach at the University of Pittsburgh where i'm a grad student going for my Ph.D. If you ever have any other questions, feel free to PM me.

I'm learning C# myself a bit, just released a new program written in it over on aximsite. My programs were on the mainpage a while back, the link is http://www.cs.pitt.edu/~jmisurda/ppc if you want to see.

Jon

Typhoon
07-11-2005, 06:51 AM
Ohhh ok. Thanks, I get it. Yea, actually that was the way I was looking at it long before...somehow I adopted some sort of wierd curiosity about it and felt like asking that question. So you teach and you are new here? Are you new w/PPCs? I have had mine for, at the most, 4 years.

I'm hardly new, but these two statistics don't really mesh:

Joined: 11 Oct 2002
Posts: 21

I'm not much of a poster 8) .

I've had a Jornada 548, Axim x5, and now an Axim x50v, so however many years that is... 4 or 5 maybe.

I teach at the University of Pittsburgh where i'm a grad student going for my Ph.D. If you ever have any other questions, feel free to PM me.

I'm learning C# myself a bit, just released a new program written in it over on aximsite. My programs were on the mainpage a while back, the link is http://www.cs.pitt.edu/~jmisurda/ppc if you want to see.

Jon

Ohhh I see. So you just read the posts here and there. Yea, I wasn't really posting much before, but somehow I just got started. Hey thanks, yea it helps to get the right guidance.

Kowalski
07-11-2005, 06:52 AM
think of this way: if main() may be "non static", than you had to create an instance of main befoure the program execution starts. but who will create an instance of main? there must be entry point that the OS must redirect the execution path

Typhoon
07-11-2005, 07:55 AM
Ohhhh I see...

Janak Parekh
07-11-2005, 03:47 PM
Ohhh ok. Thanks, I get it. Yea, actually that was the way I was looking at it long before...somehow I adopted some sort of wierd curiosity about it and felt like asking that question.
Depending on how much of a Java programmer you want to be, mastering static will ultimately be critical. For example, the following code will not work:

public class Foo {
int i;

public static void main(String[] args) {
i = 30;
}
}

... because main() only exists in a static context, but i is an instance variable and needs an object of type Foo to be created before it's available within that object.

However, it's not something that's trivial to explain on a webboard. It takes me a few lectures with lots of whiteboard discussion to drill the concept into people's heads. ;)

--janak

p.s. As a footnote, I assume you know that the signature for main is different on the two platforms, e.g., Java's is lowercase.

JonMisurda
07-11-2005, 04:50 PM
I believe main is capitalized in C#

Jon

Janak Parekh
07-11-2005, 06:30 PM
I believe main is capitalized in C#
D'oh. :oops: I should have read the subject line more closely. Nevermind. ;) I'll edit my post.

--janak

Guest979
12-20-2005, 03:11 AM
I think maybe you were thinking of "synchronized." Isn't that the Java keyword that prevents more than one thread from running the same method simultaneously? I'm not sure. I know C#, but I don't know what the C# equivalent of synchronized is.

Typhoon
12-20-2005, 06:16 AM
I think maybe you were thinking of "synchronized." Isn't that the Java keyword that prevents more than one thread from running the same method simultaneously? I'm not sure. I know C#, but I don't know what the C# equivalent of synchronized is.

Oh no...static. It does not allow a variable/function to be recreated for an instance. All instances of the class share the same "static" function/var.

Guest979
12-20-2005, 06:44 PM
Oh no...static. It does not allow a variable/function to be recreated for an instance. All instances of the class share the same "static" function/var.

Perhaps my phrasing was unclear, but actually I was correct (http://java.sun.com/docs/white/langenv/Threaded.doc2.html).

You are correct about static - it means that all instances share a method or variable. But the original post also referenced the idea of methods within a class not being allowed to execute simultaneously, and that is indeed what "synchronized" is for.

It looks like C# uses the lock (http://support.microsoft.com/default.aspx?scid=kb;en-us;816161) keyword as one of its synchronization techniques.

Typhoon
12-20-2005, 08:01 PM
Ohhh I see. Hey thanks. Sounds like something I can take a look at...

Guest979
12-21-2005, 03:48 AM
No prob. By the way, I hope you all don't mind about me bumping a few older threads in this forum, but I'm new here, and I actually became pretty knowledgeable about C# and .NETcf over the past few months. I saw several issues that were never answered, but were ones that I had encountered myself.

Typhoon
12-21-2005, 03:57 AM
No prob. By the way, I hope you all don't mind about me bumping a few older threads in this forum, but I'm new here, and I actually became pretty knowledgeable about C# and .NETcf over the past few months. I saw several issues that were never answered, but were ones that I had encountered myself.

lol nahhhh...it's okay. You can bump as many threads as you want. I think that some people out there get some ridiculous idea as if there is a certain # of post you can make or reply to, which is stupid. I don't mean you, the viewer, I am talking about people who complain. Which probaly makes sense, right? This is a forum that is on 24/7, so you can do pretty much whatever you want...