Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For what specific reason does the Java language initialize the fields of objects automatically?

"The Java language automatically initializes fields of objects, in contrast to local variables of methods that the programmers are responsible for initializing. Given what you know of intra- and inter-procedural data flow analysis, explain why the language designers may have made these design choices."

Its obvious to me that its to prevent a bug. However, what exactly is that bug? Would it be to condense the possible control flow of some given method?

Could someone go into greater detail on this? I'd really appreciate the help.

like image 969
VitaminYes Avatar asked Apr 19 '11 19:04

VitaminYes


People also ask

What do you mean by auto initialization in Java?

Automatic initialization in Java Java does not initialize non-array local variables (also referred to as automatic variables) . The Java compiler generates error messages when it detects attempts to use uninitialized local variables. The Initializer program shows how automatic initialization works.

Why do we initialize variables in Java?

Java designers believe every variable should be properly initialized. To initialize a variable is to give it a correct initial value. It's so important to do this that Java either initializes a variable for you, or it indicates an error has occurred, telling you to initialize a variable.

How do you initialize in Java?

You can initialize the variable by specifying an equal sign and a value. Keep in mind. You can initialize the variable by specifying an equal sign and a value. Keep in mind that the initialization expression must result in a value of the same (or compatible) type as that specified for the variable.

What are fields in Java?

A field is a class, interface, or enum with an associated value. Methods in the java. lang. reflect. Field class can retrieve information about the field, such as its name, type, modifiers, and annotations.


2 Answers

It's really easy to do intra-procedural data flow, so it's really easy to check whether a field has been initialized and give warnings if it hasn't (one can write a simplistic decidable algorithm, e.g. make sure all branches of an if initialize a variable, and if one branch doesn't, fail, even if the branch is unreachable).

It's really hard to do inter-procedural data flow, so it's really hard to check whether a field of an object has ever been initialized anywhere in the code (you quickly get into undecidable territory for any reasonable approximation).

Thus Java does the former and gives compile-time errors when it detects uninitialized local variables, but doesn't do the latter and initializes an object's fields to their defaults.

like image 61
Claudiu Avatar answered Nov 15 '22 09:11

Claudiu


It is not always the case that they are initialized. Objects can be instantiated without invoking any constructor by using reflections in combination with the class sun.misc.Unsafe or ObjectInputStream to access these classes private native methods or directly through JNI. These are intended for the purpose of object serialization/deserialization, and expect the fields to be populated by the deserializer. As for why the designers would have chosen to eliminate direct access to these methods(ie. without reflections) it stands to reason that pointers still left in memory could be used for stack-smashing or return-to-lib-c attacks. Clearing memory allocated for these "automatically" for most programs reduces the security risk as well as reducing the chance for errors. Also note that an attempt to read a local variable that has not been initialized results in a compile error for much the same reason

like image 25
S E Avatar answered Nov 15 '22 09:11

S E