Anıl Şenocak Anıl Şenocak
Yazılım Mühendisi
Cover image
2026-07-11 0 toplam yorum
How to Change Private Static Final Fields

In general, static means "associated with the type itself rather than an instance of the type".

This means we can have a static variable without creating an instance of the type, and any code accessing the variable accesses exactly the same data. Example:

Test x = new Test();
Test y = new Test();
x.sinifDegiskeni = 10;
y.sinifDegiskeni = 20;
System.out.println(x.sinifDegiskeni);

It prints 10 to the console: y.sinifDegiskeni and x.sinifDegiskeni are separate, because x and y reference different objects. You can refer to static members via references, but doing so is a bad idea. If we did:

Test x = new Test();
Test y = new Test();
x.statikDegisken = 10;
y.statikDegisken = 20;
System.out.println(x.statikDegisken);

then it prints 20. There is only one variable that is constant across all instances. Writing it like this would be clearer:

Test x = new Test();
Test y = new Test();
Test.statikDegisken = 10;
Test.statikDegisken = 20;
System.out.println(Test.statikDegisken);

This makes the behavior much more explicit. Modern IDEs usually suggest converting the second example to the third.

There is no reason to have an inline declaration with a value defined as below, because each instance will have its own NUMBER, but it will always have the same value (it is immutable and initialized with a literal value). This is the same as having only one final static variable for all instances.

private final int NUMBER = 10; Therefore, if it cannot change, there is no point in having an instance of it in every instance. However, it makes more sense if it is set in a constructor like this:

// Tanımlandığında setlenmek zorunda değiliz
private final int cevap;

public Bilgi(int cvp) { 
   // Yapıcı metod ile değer atanabilir ancak daha sonra değiştirilemez.
   cevap = cvp;
}

Now, we can have a different but immutable number value for each instance of the Bilgi class.

Until enums became available in Java 5, static final was the common way to define constants.

public class Bilgi {
    private static final Integer CEVAP = 42;

    public String soruSor(String soru) {
        return "Sorulan soru:" + soru + ", cevap: " + CEVAP;
    }
}

Now we want to change the answer for testing purposes:

public class BilgiTest {
    @Test
    public void testSoruSor() throws Exception {
        Bilgi bilgi = new Bilgi();

        String cevap = bilgi.soruSor("örnek soru?");
        assertThat(cevap, is("Sorulan soru:örnek soru?, cevap: 42"));

        setFinalStaticField(Bilgi.class, "CEVAP", 41);

        cevap = knowledge.askQuestion("örnek soru?");
        assertThat(cevap, is("Sorulan soru:örnek soru?, cevap: 41"));
    }

    private static void setFinalStaticField(Class<?> clazz, String fieldName, Object value)
            throws ReflectiveOperationException {

        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);

        Field modifiers = Field.class.getDeclaredField("modifiers");
        modifiers.setAccessible(true);
        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, value);
    } 
}

We make this possible by making the field accessible and removing the final modifier. After that, the field can be edited using reflection.

static initialization block
{
    // Bişeyler bişeyler...
}

It is called every time an instance of the class is created. The static block is called only once when the class itself is initialized, regardless of how many objects of this type you create.

public class Test {

    static{
        System.out.println("Static");
    }

    {
        System.out.println("static olmayan block");
    }

    public static void main(String[] args) {
        Test t = new Test();
        Test t2 = new Test();
    }
}
Static
static olmayan block
static olmayan block
Yorumlar
Henüz yorum yok. İlk yorumu sen yaz.
Yorum Bırak
Adınız (isteğe bağlı)
Yorumunuzu yazın...
0/500