Tuesday, May 25, 2021

Java Static variables

Static variables are declared using static modifier. These variables doesn't belong to any particular object, they belong to class and can be accessed by just using the class name. These variables are common to their class objects. below is a tiny example of static variable usage in java.


public class DemoStaticVar {

    static int STVAL = 300;
    
    static
    {
        STVAL = 400;
    }

    public static void main(String args[])
    {
        System.out.printf("Class variable STVAL's value is  %d",DemoStaticVar.STVAL);
    }
}

In this code two types of static variable initializations are present. First one when declaring the static variable STVAL, second int the static block. The value of the STVAL depends on the order of the declaration statement and static block. The one which comes last overrides the value of the static variable. Also observe that static variable is accessed using class name like this DemoStaticVar.STVAL. In this code we can see c-style print statement with string formatitting.

Output of the above code is


Class variable STVAL's value is  400

Let's see by exchanging the order of the statements.


public class DemoStaticVar {

    static
    {
        STVAL = 400;
    }
    static int STVAL = 300;

    public static void main(String args[])
    {
        System.out.printf("Class variable STVAL's value is  %d",DemoStaticVar.STVAL);
    }
}


Now the output will be


Class variable STVAL's value is  300

Monday, May 24, 2021

Java Static imports

import statements are used to import code from other packages and classes. Java provides an option to import static elements of a class.For that you have to include the static keyword in the import statement as shown in the below code, this feature is called Static Imports.

In this tiny java bit we will see an example of Static Imports.



import static java.lang.System.*;
import static java.time.LocalDateTime.*;
import java.time.format.DateTimeFormatter;

public class DemoStaticImport {
    public static void main(String args[])
    {
            out.println(now().format(DateTimeFormatter.ISO_DATE));
    }
}


In this example we have two static imports one from System class and the other from LocalDateTime class. if you have observed the code carefully, out.println is used instead of System.out.println. Instead of using LocalDateTime.now() we can access the static method now() without the class name because of the static import.

This code output's today's date in ISO_DATE format.


2021-05-24

Java function overloading

This tiny bit is about function overloading.

function name along with it's parameters is part of function signature. Function overloading is a feature where you can declare multiple functions with same name but different number and type of parameters. Here differentiation by just return type is not possible.


public class DemoMainOverload {

    public static void main(String args[])
    {
        System.out.printf("%s \n",main());
        DemoMainOverload obj = new DemoMainOverload();
        System.out.println(obj.main(4));
    }

    public static String main()
    {
        return "Main method overloading";
    }

    public String main(int val)
    {
        return "Instance method main with parameter "+val;
    }
}

In this code snippet main method is overloaded two times. this code generates the below output.



Main method overloading 
Instance method main with parameter 4

Sunday, May 23, 2021

Simple Java Program

This is a simple java program. Java is Object Oriented programming language.

In java everything revolves around classes. Here is simple class named Sample with a main method. If you are new to programming or you don't know about main method. It's an entrypoint to your code.


public class Sample {
    public static void main(String args[])
    {
        System.out.println("Simple output string");
    }
}

This program's output will be

Simple output string