There are similar questions here but they didn't really answer my questions.
So I am curious why we can't declare the same variable twice in Java?
for example:
int a = 4;
int a = 6;
this won't really work in Java.
However in javascript, this actually works:
var a = 1;
var a = 2;
In javascript, people said that the declaration immediately got moved to the start so that it became like this:
var a;
a = 1;
a = 2;
There are similar questions here but they didn't really answer my questions.
So I am curious why we can't declare the same variable twice in Java?
for example:
int a = 4;
int a = 6;
this won't really work in Java.
However in javascript, this actually works:
var a = 1;
var a = 2;
In javascript, people said that the declaration immediately got moved to the start so that it became like this:
var a;
a = 1;
a = 2;
Share
Improve this question
edited Dec 3, 2015 at 0:33
Marco Bonelli
69.8k21 gold badges127 silver badges146 bronze badges
asked Dec 3, 2015 at 0:03
whaleswhales
8271 gold badge14 silver badges24 bronze badges
3
- 4 not downvoting, but this is a subjective question. It's just the way the creators of java decided to write the java piler and language syntax. – Andy Guibert Commented Dec 3, 2015 at 0:07
- thanks! i guess it is kinda subjective – whales Commented Dec 3, 2015 at 0:15
- @aguibert Subjective questions are not automatically bad questions. blog.stackoverflow./2010/09/good-subjective-bad-subjective – corsiKa Commented Dec 3, 2015 at 0:24
3 Answers
Reset to default 5The simple, obvious answer is because the piler doesn't let you. But now let's go a step further - why would this be desired?
The reason here is that declaring a variable twice is a sign of a mistake. It usually means one of three things:
- Your variable names are not specific enough. Perhaps you used
int length
twice and it barks at you. You probably should make your name more specific to what it holds the length of, for exampleint originalLength
andint extendedLength
when copying an array or something. - Your method is too long. Why is your method so long that you need two of the same variable? Chances are you're duplicating code, so consolidate that into a method.
- You haven't really thought out your method. This is sort of an extension of number 2, but the truth is you should decide what a method does before you write it. If you're adding a variable that already exists, it probably means that you haven't decided exactly what this method is doing.
Each of those is a major code smell, and is probably the source of bugs down the road. (And not far down the road!) In each of the cases, allowing you to declare a variable twice is going to cause ambiguity that would have been prevented if it stopped you from piling.
Now, does this mean there aren't cases where it might be nice? Sure. There might be. Maybe you've covered all your bases and you're absolutely sure it's okay to reuse that variable. In that case, just reassign it instead of redeclaring it. Personally, I'd advise against that, but it's your foot to shoot if you want to. :)
You can use the same variable name if the scopes don't overlap, for instance i could have a variable in a private method called "var1" and then in another method have the same thing, these two would not conflict
However since everytime i use "int var1" in the same scope, java is re-declaring the variable, it wont allow it, as it's a conflicting variable name, whereas in java script the declaration happens once, as it's weakly typed
now it has been rectified or improvised in javascript too with the new let keyword if you try to intialize the same variable name more than once it will throw an error
let a = 4;
let a = 5;
will throw an error in ES6
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745316560a4622246.html
评论列表(0条)