In Java if we declare primitive variable (not local) like int, float etc and do not initialize them, then they get initialized by their default values. Can we achieve something like this for reference variables? For example if I have an employee class which contains two int variables and in another class if I have created only reference of the employee class so is it possible that int variables get initialized by zero?
-
2Can you give a code example? The terminology you're using is fairly imprecise, so much so that it's hard to answer your question.T.J. Crowder– T.J. Crowder2015-09-16 17:55:53 +00:00Commented Sep 16, 2015 at 17:55
-
I see there there might be misunderstanding between reference & object. Basically references and objects are different. references hold the object of same or sub type. references get space in stack and object gets space in heap. And I would refer you to google it to collect more concept regarding it.Subhrajyoti Majumder– Subhrajyoti Majumder2015-09-16 17:58:54 +00:00Commented Sep 16, 2015 at 17:58
-
rest assured, you will not read garbage, random data in a java program. everything is always zeroed first.ZhongYu– ZhongYu2015-09-16 18:03:08 +00:00Commented Sep 16, 2015 at 18:03
3 Answers
In Java if we declare primitive variable (not local) like int, float etc and do not initialize them, then they get initialized by their default values.
No, they don't. Fields do, both instance fields and class fields, but not variables.
Can we achieve something like this for reference variables?
Reference fields get the same behavior. The value they're initialized with is null.
For example if I have an employee class which contains two int variables and in another class if I have created only reference of the employee class so is it possible that int variables get initialized by zero?
If by "created a reference" you mean you've created an Employee instance, then any fields in that instance will be initialized to their defaults unless the Employee constructor gives them a different value.
Comments
Fields are initialized to null if they are from some "type" (other than primitive types), anyway if you provide default values in your "default" constructor for that class in particular you can override the default null values for those.
final class Employee {
private String firstName;
private String lastName;
// You can have more constructors here
public Employee() {
firstName = "Default Value for First Name";
lastName = "Default Value for Last Name";
}
// Getters, Setters
}
Comments
Only not local fields are initialized to default values when not explicitly initialized. So, something like this
public class Main {
boolean flag;
void foo(){
System.out.println(flag);
}
}
compiles and foo() method would print false, while something like this
public class Main2 {
void foo(){
boolean flag;
System.out.println(flag);
}
}
wouldn't even compile. Furthermore, default value for a reference is null.
What you can do is designing your class in such a way that, when class is initialized, a value is assigne to its instance variables, something like
public class Main {
boolean flag;
{
flag=true;
}
void foo(){
System.out.println(flag);
}
}