java - why i can make few file object in for loop at java
问题描述
In the following code I can make f object 4 times without error
for(i=0;i<3;i++){
File f2=new File("D:/");
}
but java take error for this code for second line because one time we maked object f
File f = new File("D:/");
File f = new File("C:/");
why in lopp we can make file object many times with the same name and the same constructor but at second script we can't do it?
解决方案
Your loop is equivalent to the following code:
{ // block 1 start
File f2 = new File("D:/");
} // block 1 end
{ // block 2 start
File f2 = new File("D:/");
} // block 2 end
{ // block 3 start
File f2 = new File("D:/");
} // block 3 end
Each f2
variable is limited to it's block and is only existent inside of it. However if you try to declare one variable two times in one block it will fail.
推荐阅读
- php - SOAP-ERROR:编码:对象没有“输入”属性。Laminas 肥皂客户端
- angular - 如何在路线更改时自动滚动到页面顶部 - Angular12
- python - 在数据框中更改某些条件时获取行
- sql - 有没有办法计算 PostgreSQL 中的列组合?
- javascript - 类型“(CSSProperties | undefined)[]”不可分配给类型“属性”
' - openshift - 我应该为我的基础设施节点创建一个子网,为我的应用程序节点创建另一个子网,还是为这两个节点分配一个子网?
- android-studio - 当我创建一个“空的撰写活动”项目并单击运行时,它给了我以下错误
- apache-spark - 即使在重启策略之后,带有 Kubbernetes 的 Spark 驱动程序 pod 也不会重启:总是
- django - 如何编写仅匹配 DateTime 列的分钟字段的 Django 查询?
- javascript - Django Overriden Admin Template 不导入 JS