Initialize variable with Method


1. Static
======================================

public class TestClass3 {

    static int x = me();

    static {
        x = x + 10;
        System.out.println("Static Block : "+x);
    }

    static int me(){
        System.out.println("Staic Instance");
        return 10;
    }
    public static void main(String[] args){
        System.out.println("Main Method");
    }
}
******************OUTPUT*****************
Staic Instance
Static Block : 20
Main Method


======================================

2. Non-Static
======================================
public class TestClass4 {

    int i = me();
    private int me() {
        return 20;
    }
    public static void main(String[] args){
        System.out.println("Main Method : "+new TestClass4().i);
    }
}
======================================

Comments

Popular posts from this blog

Java : String