+ 10
Basil So that I'm clear, are you asking why are static members accessible via object / instance references in Java, but not in C#? If so, then it's because the Java compiler doesn't have any compiler checks on static members being accessed via instance references, whereas the CS compiler does have a compile time check. In other words, this type of access would be possible if the CS compiler did not prevent it. This was one of several departures from Java's conventions Anders Heljsberg made when designing C#. Funny thing is Java developers will ask this question the other way, as in, why can't you access static members via instances in C#. 😉
18th Mar 2019, 1:49 PM
David Carroll
David Carroll - avatar
18th Mar 2019, 11:59 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 12
Basil 👍 Here's one simple example, I hope, to all of us will be clearer how to use static keywords! Happy Learning! ;) https://code.sololearn.com/cfHBoWu6YxmQ/?ref=app
18th Mar 2019, 1:49 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 9
Basil You are a very welcome friend!👍 Thank you for such a nice words and support 🍻 I was hoping that this example would help you to look a bit better at static keyword. As you say, a small reminder.✌😉
18th Mar 2019, 12:35 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 6
David Carroll do both Java and C# allow an instance member and a static member to share the same name?
19th Mar 2019, 12:20 AM
Sonic
Sonic - avatar
+ 3
Basil That's trippy. The object reference will still have static bindings even when the instance is null. It makes sense. But, yeah, trippy. 😜 It would also be a mess if you unintentionally call a static member thinking it was an instance member.
18th Mar 2019, 11:45 PM
David Carroll
David Carroll - avatar
+ 3
Sonic Actually... now that you mention it, the compiler wouldn't allow shared names in either language. 👍
19th Mar 2019, 2:29 AM
David Carroll
David Carroll - avatar
0
You can Acces it because the Defualt (Acces level) is public
18th Mar 2019, 11:40 AM
Vanessa Nilsson
0
When you make the field static it belongs to the class so you no need to create an object, but you can still choose if you wanna access it through the instance of the class or create an object, thats why you still can access it through both and no errors occurs
18th Mar 2019, 11:57 AM
JavaBobbo
JavaBobbo - avatar
0
Giieuwysmgtorhzhoxfwgw Gurupada Sasmal Edit: Please don't spam on SoloLearn. If you dont have anything productive to post, dont post anything at all. Continued spam may result in the deactivation of your account. Happy coding.
18th Mar 2019, 3:48 PM
Gurupada Sasmal
Gurupada Sasmal - avatar