I was wondering whether I should initialize class members in java with an initial value and then change that value to some other given value in the constructor, or should I avoid doing such a thing?
code example
public class Test {
private int value = 5;
public Test(int value) {
this.value = value;
}
}
An int can't be null .
What reason is there to use null instead of undefined in JavaScript? Javascript for Web Developers states "When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to null as opposed to anything else.
Considering we say about C++ (now your question placed in the C++ section) compiler does not need variables be initialized to 0. But it do that automaticaly for arithmetic types defined in a global scope f.e. That also does not mean that someone need it.
First, you cannot initialize an int value with null. The default value of an int is zero, but initializing it in both the field declaration and the constructor is pointless. Assuming the compiler does not omit the field initializer completely, it effectively compiles to this:
Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style. Can variables be used in Java without initialization?
Answer Wiki. Yeah in Java , uninitialized variables automatically initialized to zero but if the variable is an instance variable and a class variable because if we don’t initialize the instance variable the system by default initialize them to their respective default values. Following are the instance variable with their default values.
1. Default Initialization of Instance Variables in Java When you declare a variable without assigning it an explicit value, the Java compiler will assign a default value. Let’s consider the following example: NOTE: This default initialization applies for instance variables, not for method variables.
If not specified,:
primitive byte
s, short
s, int
s, long
s, float
s and double
s are initialized to 0
boolean
s are initialized to false
Objects are initialized to null
If we are talking about class fields than all unset variables of
0
(numeric ones like int
, long
, double
...)\u0000
(char
)false
(boolean
). String
, Integer
, or AnyOtherClass
are set to null
so actually it doesn't matter if you set it explicitly, because
private int x;
private Integer y;
is equivalent of
private int x = 0;
private Integer y = null;
Java give value class variables, I mean they are initialized by JVM and you can use them. But you must to search their default values to use them correctly.
On the other hand, JVM does not initialize the local variables which is created in methods. So if you create any variable on methods you have to assign them to a value before use them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With