java 两个普通代码块声明了同名变量 为什么不会冲突
{WFParticiparticiU=newWFPartici();particiU.setId(groupid);particiU.setName("B");parti...
{
WFPartici particiU = new WFPartici();
particiU.setId(groupid);
particiU.setName("B");
particiU.setTypeCode("organization");
particis.add(particiU);
}
{
WFPartici particiU = new WFPartici();
particiU.setId(roleType);
particiU.setName("B");
particiU.setTypeCode("role");
particis.add(particiU);
}
两个代码块都声明了particiU 这个对象,编译正常求解释 展开
WFPartici particiU = new WFPartici();
particiU.setId(groupid);
particiU.setName("B");
particiU.setTypeCode("organization");
particis.add(particiU);
}
{
WFPartici particiU = new WFPartici();
particiU.setId(roleType);
particiU.setName("B");
particiU.setTypeCode("role");
particis.add(particiU);
}
两个代码块都声明了particiU 这个对象,编译正常求解释 展开
3个回答
展开全部
在Java中,变量根据作用域分为全局变量和局部变量,不同类型变量的作用范围不一样;例如:
@BizComponent
public class AddElementServiceImpl implements IAddElementService {
@Autowired
private IHibernateDao hibernateDao;
@Autowired
private IProcessmanualService processmanualService;
@Autowired
private IQueryElementService queryElementServiceImpl;
public boolean addGroup(GroupModel groupModel) {
// TODO Auto-generated method stub
Dao dao = new Dao();
int i = hibernateDao.executeSqlUpdate(SQL.INSERT_GROUP + "('"
+ groupModel.getGroupId() + "','" + groupModel.getParentId()
+ "','" + groupModel.getName() + "','" + groupModel.getType()
+ "','" + groupModel.getCreateUser() + "')");
boolean bs = false;
if (i == 1)
bs = true;
return bs;
}
public boolean addGroups(List<GroupModel> groupModels) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Dao dao = new Dao();
int i = hibernateDao.executeSqlUpdate(SQL.INSERT_GROUP + "('"
+ groupModel.getGroupId() + "','" + groupModel.getParentId()
+ "','" + groupModel.getName() + "','" + groupModel.getType()
+ "','" + groupModel.getCreateUser() + "')");
boolean bs = false;
if (i == 1)
bs = true;
return bs;
}
}
在以上代码中,变量hibernateDao是全局变量,它的作用范围是整个AddElementServiceImpl类,所有AddElementServiceImpl类中,所有的hibernateDao都是同一个变量,当对AddElementServiceImpl类的调用结束时,GC才会将hibernateDao回收;而方法addGroup和addGroups中声明的变量dao是局部变量,当对方法addGroup进行调用时,JVM声明并使用dao变量,当对方法addGroup的调用结束后,GC将对dao进行回收,接下来再调用方法addGroups时,JVM将重新声明并使用dao变量。
起始这个问题就是Java中变量的作用域问题,你平时在看书的时候多注意了解一下就行了,在实际工作中这是最基础的问题
@BizComponent
public class AddElementServiceImpl implements IAddElementService {
@Autowired
private IHibernateDao hibernateDao;
@Autowired
private IProcessmanualService processmanualService;
@Autowired
private IQueryElementService queryElementServiceImpl;
public boolean addGroup(GroupModel groupModel) {
// TODO Auto-generated method stub
Dao dao = new Dao();
int i = hibernateDao.executeSqlUpdate(SQL.INSERT_GROUP + "('"
+ groupModel.getGroupId() + "','" + groupModel.getParentId()
+ "','" + groupModel.getName() + "','" + groupModel.getType()
+ "','" + groupModel.getCreateUser() + "')");
boolean bs = false;
if (i == 1)
bs = true;
return bs;
}
public boolean addGroups(List<GroupModel> groupModels) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Dao dao = new Dao();
int i = hibernateDao.executeSqlUpdate(SQL.INSERT_GROUP + "('"
+ groupModel.getGroupId() + "','" + groupModel.getParentId()
+ "','" + groupModel.getName() + "','" + groupModel.getType()
+ "','" + groupModel.getCreateUser() + "')");
boolean bs = false;
if (i == 1)
bs = true;
return bs;
}
}
在以上代码中,变量hibernateDao是全局变量,它的作用范围是整个AddElementServiceImpl类,所有AddElementServiceImpl类中,所有的hibernateDao都是同一个变量,当对AddElementServiceImpl类的调用结束时,GC才会将hibernateDao回收;而方法addGroup和addGroups中声明的变量dao是局部变量,当对方法addGroup进行调用时,JVM声明并使用dao变量,当对方法addGroup的调用结束后,GC将对dao进行回收,接下来再调用方法addGroups时,JVM将重新声明并使用dao变量。
起始这个问题就是Java中变量的作用域问题,你平时在看书的时候多注意了解一下就行了,在实际工作中这是最基础的问题
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
变量作用域不同
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询