Java Puzzle 31 – Initialize Before
Merhaba Arkadaslar
Ornek kodumuzu inceleyelim;
Reluctant.java
package _31.catch$.constructor; public class Reluctant { private Reluctant internalInstance = new Reluctant(); public Reluctant() throws Exception { throw new Exception("I'm not coming out"); } public static void main(String[] args) { try { Reluctant r = new Reluctant(); System.out.println("Surprise!"); } catch (Exception ex) { System.out.println("I told you so"); } } }
Kodumuzu inceleyecek olursak main metodunda Reluctant objesi olusturmaktayiz ve Reluctant constructor Exception firlatmaktadir. Bu Exception icin handle yaklasimini (try/catch) kullandik.
Ornegimizi calistirdigimizda ciktimiz ne olacaktir ? Constructor hata firlatacak ve catch bloguna dusecek olarak dusunebiliriz fakat boyle calismayacaktir.
Ornegimizi calistirdigimizda StackOverflowError almaktayiz. Peki neden ?
Exception in thread "main" java.lang.StackOverflowError
StackOverflowError un genel olarak temel nedeni infinite recursion’dir.Oyleyse burada teorik olarak sonsuz olarak tekrar edilen bir is var.
When you invoke a constructor, the instance variable initializers run before the body of the constructor.
instance variable’lar , constructordan once initialize edilmektedir. Kodumuzda soyle bir ifade yer almaktadir.
Bu ifade constructor’i recursive olarak cagiracaktir. Cunku yeni obje olusturmak istediginde yine ayni kod blogu yine var olacaktir. Tekrar tekrar calismak isteyecek ve sonunda StackOverflowError a neden olacaktir.
private Reluctant internalInstance = new Reluctant();
Bununla birlikte try-catch blogunda Exception’i yakalamaktayiz peki neden StackOverflowError yakalanmiyor ?
Bunun nedeni StackOverflowError bir Expcetion degildir. Error ‘dir.
java.lang.Exception ve java.lang.Error hiyerarsi olarak IS-A java.lang.Throwable dir.
Bir class’ta kendi tipinde obje olusturmak pek yaygin degildir. Bununla birlikte bazi gerekli olan veri yapilari icin ornegin linked list nodes , tree nodes gibi obje olusturken bu probleme dikkat edilmelidir.
Bir baska ornek olarak su kodumuzu inceleyelim ;
Car.java
package _31.catch$.constructor; public class Car { int hp = getEngineHP(); private int getEngineHP() throws Exception { return 100; } }
hp instance variable’ina deger atamasi icin getEngineHP metodunu cagirmaktayiz fakat bu metot checked exception olan java.lang.Exception firlatmaktadir. Bu durumda kodumuz derleme hatasi verecektir ve instance variable’da bu noktada handle or declare kuralini uygulamayamiyiz.
instance initializers run before constructor bodies. Any exceptions thrown by instance initializers propagate to constructors. If initializers throw checked exceptions, constructors must be declared to throw them too, but this should be avoided, because it is confusing
Burada su sekilde bir uyari mesaji vermektedir;
Default constructor cannot handle exception type Exception thrown by implicit super constructor. Must define an explicit constructor
Burada acik olarak Constructor tanimlamali ve ilgili Exception’i declare etmeliyiz.
Car.java
package _31.catch$.constructor; public class Car { /* * if constructor is not declared ; * * Default constructor cannot handle exception type Exception thrown by * implicit super constructor. Must define an explicit constructor */ int hp = getEngineHP(); private int getEngineHP() throws Exception { return 100; } public Car() throws Exception { } }
Github kaynak kodlar / Source Code ;
leventerguder/injavawetrust-puzzler
Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer
Leave a Reply