+ 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#. 😉
+ 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
+ 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.✌😉
+ 6
David Carroll do both Java and C# allow an instance member and a static member to share the same name?
+ 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.
+ 3
Sonic Actually... now that you mention it, the compiler wouldn't allow shared names in either language. 👍
0
You can Acces it because the Defualt (Acces level) is public
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
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.



