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!
=== Fields and initialization === {| class="wikitable" style="width:80%;" |- ! style="width:40%;"| Fields and initialization !! style="width:30%;"|Java !! style="width:30%;"|C# |- |[[Field (computer science)|Fields]] || {{yes}} || {{yes}} |- |[[Constant (programming)|Constants]] || {{yes}} || {{yes}}; but no support for constant passed parameters<ref name="final">{{cite web |url=http://csunwold.blogspot.co.at/2010/02/from-stackoverflow-im-looking-for-c.html |title=C# Equivalent to Java's "final" |last1=Sunwold |first1=Corey |date=25 February 2010 |publisher=Corey Sunwold |archive-url=https://archive.today/20121129011204/http://csunwold.blogspot.co.at/2010/02/from-stackoverflow-im-looking-for-c.html |url-status=live |archive-date=29 November 2012 |quote=There is than one use of the final keyword that C# does not have an equivalent for. When you pass a parameter to a method in Java, and you don't want that parameter's value to change within the scope of that method you can set it as final like this: |access-date=13 September 2016 }}</ref> |- |Static (class) [[Constructor (object-oriented programming)|constructors]] || {{yes}} || {{yes}} |- |Instance constructors || {{yes}} || {{yes}} |- |[[Destructor (computer science)|Finalizers/destructors]] || {{yes}} || {{yes}} |- |Instance initializers || {{yes}} || {{no}}; can be simulated with instance constructor |- |Object [[Initialization (programming)|initialization]] || {{yes|Bottom-up <br/>(fields and constructors)}} || {{yes|Top-down (fields); bottom-up (constructors)}} |- |Object initializers || {{yes}} || {{yes}} |- |Collection initializers || {{yes|Static varargs methods}} || {{yes}} |- |Array initializers || {{yes}} || {{yes}} |} ==== Object initialization ==== In both C# and Java, an object's fields can be initialized either by ''variable initializers'' (expressions that can be assigned to variables where they are defined) or by ''constructors'' (special subroutines that are executed when an object is being created). In addition, Java contains ''instance initializers'', which are anonymous blocks of code with no arguments that are run after the explicit (or implicit) call to a superclass's constructor but before the constructor is executed. C# initializes object fields in the following order when creating an object: # Derived static fields # Derived static constructor # Derived instance fields # Base static fields # Base static constructor # Base instance fields # Base instance constructor # Derived instance constructor Some of the above fields may not be applicable (e.g. if an object does not have ''static fields''). ''Derived fields'' are those that are defined in the object's direct class, while ''base field'' is a term for the fields that are defined in one of the object's superclasses. Note that an object representation in memory contains all fields defined in its class or any of its superclasses, even, if some fields in superclasses are defined as private. It is guaranteed that any field initializers take effect before any constructors are called, since both the instance constructor of the object's class and its superclasses are called after field initializers are called. There is, however, a potential trap in object initialization when a virtual method is called from a base constructor. The overridden method in a subclass may reference a field that is defined in the subclass, but this field may not have been initialized because the constructor of the subclass that contains field initialization is called after the constructor of its base class. In Java, the order of initialization is as follows: # Invocation of another constructor (either of the object's class or of the object's superclass) # Instance variable initializers and instance initializers (in the order they appear in the source code) # The constructor body Like in C#, a new object is created by calling a specific constructor. Within a constructor, the first statement may be an invocation of another constructor. If this is omitted, the call to the argumentless constructor of the superclass is added implicitly by the compiler. Otherwise, either another overloaded constructor of the object's class can be called explicitly, or a superclass constructor can be called. In the former case, the called constructor will again call another constructor (either of the object's class or its subclass) and the chain sooner or later ends up at the call to one of the constructors of the superclass. After another constructor is called (that causes direct invocation of the superclass constructor, and so forth, down to the Object class), instance variables defined in the object's class are initialized. Even if there are no variable initializers explicitly defined for some variables, these variables are initialized to default values. Note that instance variables defined in superclasses are already initialized by this point, because they were initialized by a superclass constructor when it was called (either by the constructor's code or by variable initializers performed before the constructor's code or implicitly to default values). In Java, variable initializers are executed according to their textual order in the source file. Finally, the constructor body is executed. This ensures proper order of initialization, i.e. the fields of a base class finish initialization before initialization of the fields of an object class begins. There are two main potential traps in Java's object initialization. First, variable initializers are expressions that can contain method calls. Since methods can reference any variable defined in the class, the method called in a variable initializer can reference a variable that is defined below the variable being initialized. Since initialization order corresponds to textual order of variable definitions, such a variable would not be initialized to the value prescribed by its initializer and would contain the default value. Another potential trap is when a method that is overridden in the derived class is called in the base class constructor, which can lead to behavior the programmer would not expect when an object of the derived class is created. According to the initialization order, the body of the base class constructor is executed before variable initializers are evaluated and before the body of the derived class constructor is executed. The overridden method called from the base class constructor can, however, reference variables defined in the derived class, but these are not yet initialized to the values specified by their initializers or set in the derived class constructor. The latter issue applies to C# as well, but in a less critical form since in C# methods are not overridable by default.
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)