Example 1: Java implementation of final and static keywords
KW.java
class KW
{
    static int a=10;
    public static void main(String args[])
    {
        System.out.println(KW.a);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 10 kodingwindow@kw:~$
Example 2: Java implementation of final and static keywords
KW.java
class KW
{
    static int a=10;
    static
    {
        a=20;
    }
    public static void main(String args[])
    {
        System.out.println(KW.a);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 20 kodingwindow@kw:~$
Example 3: Java implementation of final and static keywords
KW.java
class KW
{
    static int a=9;
    static
    {
        final int a=11;
    }
    public static void main(String args[])
    {
        System.out.println(KW.a+1);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 10 kodingwindow@kw:~$
Example 4: Java implementation of final and static keywords
KW.java
class KW
{
    static int i=0;
    public KW()
    {
        i=i+1;
        System.out.println(i);
    }
    public static void main(String args[])
    {
        KW k=new KW();
        KW k1=new KW();
        KW k2=new KW();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 1 2 3 kodingwindow@kw:~$
Example 5: Java implementation of final and static keywords
KW.java
class KW
{
    static int i=0;
    static 
    {
        i=10;
    }
    public KW()
    {
        i=i+1;
        System.out.println(i);
    }
    public static void main(String args[])
    {
        KW k=new KW();
        KW k1=new KW();
        KW k2=new KW();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 11 12 13 kodingwindow@kw:~$
Example 6: Java implementation of final and static keywords
KW.java
class KW
{
    final int i=0;
    public KW(int i)
    {
        System.out.println(i+1);
    }
    public static void main(String args[])
    {
        KW k=new KW(1+1);
        KW k1=new KW(20);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 3 21 kodingwindow@kw:~$
Example 7: Java implementation of final and static keywords
KW.java
class KW
{
    static int a=10+1;
    final static int b=20+1;
    public static void main(String args[])
    {   
        a+=+b+1;
        System.out.println(KW.a+1+a);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 67 kodingwindow@kw:~$
Advertisement