Pure Java – 83 Nested Class – Static Nested Class

Merhaba Arkadaslar,
Bu bolumde Static Nested Class’lari incelecegiz. Bu yazi ile Nested Class konusunu bitirecegiz.

Static Nested Class’lara, Static Inner Class ifadesi kullanilmaktadir fakat bu ifade dogru degildir . Static Nested Classlar , Top-level nested class’tir.

public class Outer {

	static class StaticNested {
	}

}

StaticNested sinifinin kendisi static degildir, static sinif diye bir sey yoktur. static anahtar kelimesinin anlami Outer sinifinin static bir uyesi anlaminda kullanilmaktadir.Static Nested Class bir inner class degildir.

  • Bu nedenle, diger static uyeler icin oldugu gibi Outer sinifinin instance/objesi olmadan da bu static uyeye ulasilabilir. Inner classlara ulasim icin Outer objesi gereklidir.
public class Outer {

	static class StaticNested {
	}

	class InnerClass {

	}

	public static void main(String[] args) {

		Outer.StaticNested staticNested = new Outer.StaticNested();
		StaticNested staticNested2 = new StaticNested();

		Outer.InnerClass innerClass = new Outer().new InnerClass();
		InnerClass innerClass2 = new Outer().new InnerClass();
	}

}
  • Static Nested Class’lar static veya non-static metotlara sahip olabilir. Inner Class sadece non-static metoda sahip olabilir.
public class Outer {

	static class StaticNested {
		void method() {
			System.out.println("StaticNested method");
		}

		static void staticMethod() {
			System.out.println("StaticNested static method");
		}
	}

	class InnerClass {
		void method() {
			System.out.println("InnerClass method");
		}

//		static void staticMethod() {
//			System.out.println("compile error");
//		}
	}

	public static void main(String[] args) {

		Outer.StaticNested staticNested = new Outer.StaticNested();
		StaticNested staticNested2 = new StaticNested();

		Outer.InnerClass innerClass = new Outer().new InnerClass();
		InnerClass innerClass2 = new Outer().new InnerClass();
	}

}
  • Static Nested Class’lar static veya instance degisken tanimlayabilir. Static tanimli metotdan non-static instance degiskene ulasim saglanamaz.
public class Outer {

	static class StaticNested {
		private String instanceVariable = "I am instance variable";
		private static String staticVariable = "i am static variable";

		void method() {
			System.out.println(staticVariable);
			System.out.println("StaticNested method");
		}

		static void staticMethod() {
			//System.out.println(instanceVariable);
			//derleme hatasi
			System.out.println(staticVariable);
			System.out.println("StaticNested static method");
		}
	}

	public static void main(String[] args) {

		Outer.StaticNested staticNested = new Outer.StaticNested();
		StaticNested staticNested2 = new StaticNested();

	}

}
  • Static Nested sinif, Outer sinifin static degiskenlerine ve static metotlara erisim saglanabilir. Instance degiskenlere ve non-static degiskenlere erisim saglanamaz.
public class Outer {

	private String outerInstanceVariable = "i am outer instance variable";
	private static String outerStaticVariable = "i am static outer instance variable";

	private void outerTest() {
		
	}
	
	private static void outerStaticTest(){
		
	}
	
	static class StaticNested {
		void method() {
			// System.out.println(outerInstanceVariable);
			System.out.println(outerStaticVariable);
			
			//outerTest();
			//derleme hatasi
			
			outerStaticTest();
		}
	}

}

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

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *