This question already has answers here:
Why are local variables not initialized in Java?
(16 answers)
Closed 6 years ago.
instance and static variables will be initialized with a default values if we don't initialize with our own values but it doesn't happen with local variables.
Why they designed like so ?
The reason we have default values at all is that it's was decided it was too complicated to determine if a variable is initialised before it is used (unless it's final) This is because you could call methods in any order.
For local variable it can determine whether you have used a variable before you gave it a value, so this check prevents you using uninitialised values to avoid errors in your code.
Related
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I am learning java and in my learning material I found the following question:
You are creating an application that includes a number of methods. These methods will change the values of both primitive and reference variables passed in as arguments. Which statement best describes the effect that changes made in the method will have on the original variables? Choose the best option(s) from those listed below.
The question is whether the value reference type can be changed, and the answer is no. But could anyone help with showing a code which demonstrates this behavior?
Thank you
reference type cant be changed.
Java is a pass by value language.
This means when you invoke a method ,such as
Person p = new Person();
foo(p);
You are pass a copy of reference p.
So you cant change reference type,but you could change the value indicated by the reference.
This question already has answers here:
Why does BufferedInputStream copy a field to a local variable rather than use the field directly
(3 answers)
Closed 6 years ago.
In Netty I have seen object member variables assigned locally in class methods quite frequently. Is this a matter of style or is there a programmatic benefit?
I have included a code snippet below:
public ChannelFuture bind() {
validate();
SocketAddress localAddress = this.localAddress;
if (localAddress == null) {
throw new IllegalStateException("localAddress not set");
}
return doBind(localAddress);
}
I usually see that pattern when multi-threading is a concern. For example, if another thread may alter the member value or null it after the null check, yet it is still a valid use case for member access to occur at that point. Or the code is trying to avoid locks/synchronization. So instead the member is copied to a local and all further operations are done using the local copy to prevent a null access.
This question already has answers here:
Java Reflection: How to get the name of a variable?
(8 answers)
Closed 7 years ago.
I'm facing following problem: I have to give names to many thousands of GUI-Elements. Would be nice if it could work a script like:
JMenuItem myMenuItem = new JMenuItem();
myMenuItem .setName(this.getClass().getSimplename() + "." + myMenuItem .get??)
Would be nice if I could set the name of the element to "Classname.myMenuItem".
Is there any way to get the name (variable name ofc, not JMenuItem.getName()) of the variable?
You can do this for field via reflection: see Field.getName().
You cannot do it for local variable, they basically don't have any name after the code is compiled (well, in debug info they have but it's not easily accessible and understandable unless you implement debugger :-) ).
What you want is not possible. Even if some information about variable names is present in the class file, you still have to remember that there is a difference between the variable and the object it refers to. There can be any number of variables (local variables, member variables, parameters, etc) that refer to the same object. Keeping track of the names of all of those variables would impose too much of a run-time overhead.
This question already has answers here:
Difference between local variable initialize null and not initialize?
(6 answers)
Closed 7 years ago.
My code below is compile error.
The local available may not has been initialized
String str;
my_obj.setValue(str);
and fixed by this
String str = null;
my_obj.setValue(str);
So is null instance of anything? What is the difference between null and not initialized.
Why does it work for this way?
My Class
class MyClass {
String str;
}
I have initialized obj not obj.str. but no such a compile error that ways...
MyClass obj = new MyClass();
my_obj.setValue(str);
I already read what all of you recommend before I post this questions. Maybe it duplicated, but I didn't get any idea form those.
Local variables do not get default values. Their initial values are undefined with out assigning values by some means. Before you can use local variables they must be initialized.
There is a big difference when you declare a variable at class level (as a member ie. as a field) and at method level.
If you declare a field at class level they get default values according to their type. If you declare a variable at method level or as a block (means anycode inside {}) do not get any values and remain undefined until somehow they get some starting values ie some values assigned to them.
Local variables must be initialized before they are accessed.
not initialized means, that you have not created object for that instance. Assigning a value will create a object to use it.
String str ;
Above code represents, that you have just declared it. But, can't use it until, it is assigned with some real value or null.
Reference to the question could be verified on this link
This question already has answers here:
Why doesn't java support pass by reference like C++
(2 answers)
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 9 years ago.
Java is strictly pass by value..I am still confused with this concept.
I have gone through many websites to get an answer for this but I am not able to find any good reason.
Is there any proper reason why Java is not pass by reference?
The value of a reference variable is an "address" in Java. When you pass a reference variable to a method, a new reference variable is placed on the stack and a copy of the passed reference variable's value is used to initialize the new local reference varaible's value, just like any primitive type.
That is the classic definition of pass by value.
NOTE: While you can think of the reference as a memory address, it's not. The underlying mechanism makes it act logically as if it were though.
SHORT VERSION: references are simple variables just like the other primitive types for purposes of passing arguments to methods. What you can do with them once passed is obviously different.