ITEM 57: 지역변수의 범위를 최소화해라
지역 변수 유효 범위 줄이는 방법
Iterator<Element> i = c.iterator(); while(i.hasNext()){ doSomething(i.next()); } Iterator<Element> i2 = c2.iterator(); while(i.hasNext()){ // bug 위에서 선언한 i 사용 doSomething(i2.next()); }for(Iterator<Element> i = c.iterator(); i.hasNext();) { Element e = i.next(); } for(Iterator<Element> i2 = c2.iterator(); i.hasNext();) { // i를 찾을 수 없다는 컴파일 오류 발생 Element e = i2.next(); }
Last updated