Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Inner and local classes === Both languages allow ''inner classes'', where a class is defined lexically inside another class. However, in each language these inner classes have rather different semantics. In Java, unless the inner class is declared {{mono|static}}, a reference to an instance of an inner class carries a reference to the outer class with it. As a result, code in the inner class has access to both the static and non-static members of the outer class. To create an instance of a non-static inner class, the instance of the embracing outer class must be named.<ref name="inner instance creation">{{cite web |url=http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#41147 |title=Java SE Specifications |publisher=Java.sun.com |access-date=24 February 2015}}</ref> This is done via a new {{mono|new}}-operator introduced in JDK 1.3: {{code|outerClassInstance.new Outer.InnerClass()}}. This can be done in any class that has a reference to an instance of the outer class. In C#, an inner class is conceptually the same as a normal class. In a sense, the outer class only acts as a namespace. Thus, code in the inner class cannot access non-static members of the outer class unless it does so through an explicit reference to an instance of the outer class. Programmers can declare the inner class ''private'' to allow only the outer class to have any access to it. Java provides another feature called ''local classes'' or ''anonymous classes'', which can be defined within a method body. These are generally used to implement an interface with only one or two methods, which are typically event handlers. However, they can also be used to override virtual methods of a superclass. The methods in those local classes have access to the outer method's local variables declared {{mono|final}}. C# satisfies the use-cases for these by providing anonymous [[Delegate (CLI)|delegates]]; see [[#Event|event handling]] for more about this. C# also provides a feature called ''anonymous types/classes'', but it is rather different from Java's concept with the same name. It allows the programmer to instantiate a class by providing only a set of names for the properties the class should have, and an expression to initialize each. The types of the properties are inferred from the types of those expressions. These implicitly-declared classes are derived directly from ''object''.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)