How do I setup JAVA_HOME and Path variables in Windows?

Setting up a JAVA_HOME and Path variables is the second thing you’ll need to do after installing a JDK (Java Development Kit). Although this is not required by Java it self, it is commonly use by other application. For instance then Apache Tomcat web application server and other application server will need it. Or we might need it if we want to compile or running our Java classes from the command prompt. It helps us to organize the default JDK and the execution path.

So here are the steps that we’ll need to do to configure the JAVA_HOME and Path variable on a Windows operating system.

Step 1. Finding the location of our JDK installation directory. If we already know where we have installed the JDK continue to the Step 2.

  1. The JDK usually installed in the C:\Program Files\Java directory by default.
  2. Under this directory we can find one or more versions of installed JDK, for examples I have jdk1.6.0_39 and jdk1.7.0_13. Just choose the default one we’re going to use.

Step 2. Setting JAVA_HOME variable

After we know the location of your JDK installation, we can copy the directory location from the Windows Explorer address bar.

  1. Open Windows Explorer
  2. Right Click the Computer and select the Properties menu.
  3. Click Advanced system settings and the System Properties windows will be shown.
  4. Select the Advance tab.
  5. Click the Environment Variables button.
  6. A new Environment Variables window will be shown.
  7. Under the System Variables, click the New button to create a new environment variable.
  8. Enter the variable name as JAVA_HOME, all letters are in uppercase.
  9. In the variable value enter the JDK installation path you’ve copy above.
  10. Click OK.

Step 3. Setting the Path variable

After we’ve set the JAVA_HOME variable, now we can update the Path variable.

  1. In the Environment Variables window, under the System Variables section find a variable named Path.
  2. If we don’t have the Path variable we need to add one using the New button.
  3. If we already have the Path variable we’ll need to update its value, click Edit button to update.
  4. Add %JAVA_HOME%\bin; to the beginning of the Path variable value.
  5. Press OK to when we are done.
  6. Press another OK to close the Environment Variables window.

Step 4. Check to see if the settings work

  1. Open your Windows Command Prompt.
  2. Type java -version in the command line.
  3. If everything was set correctly we’ll see the running version of your installed Java JDK.

As an example on my Windows Command Prompt I have something like:

D:\>java -version
java version "1.7.0_13"
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) Client VM (build 23.7-b01, mixed mode)

If you don’t see the correct output, for instance you get an error like “‘java’ is not recognized as an internal or external command, operable program or batch file”, please retry the steps described above. Enjoy your new adventure with Java programming. Happy coding!

What is reference variable in Java?

The only way you can access an object is through a reference variable. A reference variable is declared to be of a specific type and that type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables.

A reference variable that is declared as final can’t never be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed.

package org.kodejava.example.basic;

public class ReferenceDemo {
    public static void main(String[] args) {
        // Declaration of Reference variable
        Reference ref1, ref2;

        // ref3 is declared final, ref3 can't be reassigned
        // or refer to different object
        final Reference ref3;

        // assign ref1 with object Reference
        ref1 = new Reference("This is the first reference variable", 1);

        // access method getNumber() of object Reference through
        // variable ref1
        int number = ref1.getNumber();
        System.out.println("number= " + number);

        // assign ref2 with object Reference
        ref2 = new Reference("This is the second reference variable", 2);

        // passing ref2 as method parameter of printText() method
        ReferenceDemo.printText(ref2);

        // assign ref3 with object Reference
        ref3 = new Reference("This is the third reference variable", 3);

        // try to reassign ref3 will cause a compile-time error
        // ref3 = new Reference("Try to reassign", 3);

    }

    public static void printText(Reference reference) {
        String text = reference.getText();
        System.out.println(text);
    }
}
package org.kodejava.example.basic;

public class Reference {
    private int number;
    private String text;

    Reference(String text, int number) {
        this.text = text;
        this.number = number;
    }

    public String getText() {
        return text;
    }

    public int getNumber() {
        return number;
    }
}

How do I define a constant variable?

To define a constant in Java, use final modifier which combined with static modifier. The final modifier indicates that the value of this field cannot change.

If you change the value of the constant, you need to recompile the class to get the current value. Other feature in Java that provide similar functionality is enumeration (a list of named constants). You can simply create an enumeration by using the enum keyword.

package org.kodejava.example.fundamental;

public class ConstantDemo {
    public static void main(String[] args) {
        int sunday = DayConstant.SUNDAY;
        System.out.println("Sunday = " + sunday);

        String dozen = MeasureConstant.DOZEN;
        System.out.println("Dozen  = " + dozen);
    }
}

class DayConstant {
    public final static int SUNDAY = 0;
    public final static int MONDAY = 1;
    public final static int TUESDAY = 2;
    public final static int WEDNESDAY = 3;
    public final static int THURSDAY = 4;
    public final static int FRIDAY = 5;
    public final static int SATURDAY = 6;
}

class MeasureConstant {
    final static String UNIT = "unit";
    final static String DOZEN = "dozen";
}

How do I declare and initialize variable?

Variable is a field in which object store its state. It also an allocations for the placement of data in memory. When the statement of variable declaration is compiled, some bytes of memory will be allocated for the variable. The size is determine by the type of variable.

One variable definition is able to store data only of one particular type. Before it can use, the variable must be declared. The name and type of variable must specified in variable declaration. If you want the variable to have an initial value, you must specify your own value in the declaration.
You can assign a value into variable by using an assignment statement. The assignment operator is =.

package org.kodejava.example.fundamental;

public class VariableExample {
    // declares a double variable number1 and total
    private double number1, total;

    // declares a double variable and initializes its value to 10000
    private double number2 = 1000;


    public static void main(String[] args) {
        VariableExample ve = new VariableExample();

        // assigns a value to variable number1
        ve.number1 = 500;

        // assigns the calculation result of number1 + number2 to 
        // total
        ve.total = ve.number1 + ve.number2;
        System.out.println(ve.total);
    }
}

How do I declare and initialize local variable?

Local variables are variables that are not fields of a class. A function or method often store its temporary state in local variables. Local variables only visible to the methods in which they are declared.

Local variables must be declare and initialize before it used for the first time. Local variables will not get a default value if you do not initialize it and could cause a compile-time error.

package org.kodejava.example.fundamental;

public class LocalVariableExample {
    // it's okay if total variable does not initialize.
    // it will initialize with default value = 0.
    int total;

    public static int add() {
        // this will cause compile-time error if does not initialize
        int x = 1, y = 2;

        // z is assigned by the calculation result of x + y
        int z = x + y;
        return z;
    }

    public static void main(String[] args) {
        LocalVariableExample lve = new LocalVariableExample();
        // assigns total with the result of add() method execution
        lve.total = add();
        System.out.println("total= " + lve.total);
    }
}

How do I create static variables in Java?

Class variables or static variable is variable that declared with static modifier. A given class will have only one copy of each of its static variables, regardless of how many times the class has been instantiated.

If the value of a static variable is changed, the new value is available equally in all instances of the class. The final keyword could be added to indicate the value of static variable will never change.

If you try to assign a new value to final variable, you will get a compile error.

package org.kodejava.example.fundamental;

public class StaticDemo {
    // static variable
    static int x = 12;

    // static variable with final value that never change
    final static int Y = 20;

    // non-static variable
    int z;

    public static void main(String[] args) {
        StaticDemo sd0 = new StaticDemo();

        System.out.println("x before update = " + StaticDemo.x);
        System.out.println("y= " + StaticDemo.Y);

        sd0.z = StaticDemo.x + StaticDemo.Y;
        System.out.println("z= " + sd0.z);

        StaticDemo.x = 15;
        System.out.println("x after update = " + StaticDemo.x);

        StaticDemo sd1 = new StaticDemo();
        StaticDemo sd2 = new StaticDemo();
        StaticDemo.x = 20;

        System.out.println("StaticDemo.x = " + StaticDemo.x);
        System.out.println("sd0 = " + sd0.getX());
        System.out.println("sd1 = " + sd1.getX());
        System.out.println("sd2 = " + sd2.getX());

        //
        // try to assign value to final variable, it will cause a
        // compile time error
        //
        // StaticDemo.Y = 30;
    }

    public int getX() {
        return StaticDemo.x;
    }
}

Here is the output printed by the program:

x before update = 12
y= 20
z= 32
x after update = 15
StaticDemo.x = 20
sd0 = 20
sd1 = 20
sd2 = 20

How do I get environment variables?

Environment variables are a set of dynamic values that can affect a running process, such as our Java program. Each process usually have their own copy of these variables.

Now we would like to obtain the available variables in our environment or operating system, how do I do this in Java? Here is a code example of it.

package org.kodejava.example.lang;

import java.util.Map;
import java.util.Set;

public class SystemEnv {
    public static void main(String[] args) {
        // We get the environment information from the System class. 
        // The getenv method (why shouldn't it called getEnv()?) 
        // returns a map that will never have null keys or values 
        // returned.
        Map<String, String> map = System.getenv();

        Set<String> keys = map.keySet();
        for (String key : keys) {
            // Here we iterate based on the keys inside the map, and
            // with the key in hand we can get it values.
            String value = map.get(key);
            System.out.println(key + " = " + value);
        }
    }
}

Here are some result on my machine.

PATH = /opt/local/bin:/opt/local/sbin:~bin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/gradle/gradle-2.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
SHELL = /bin/bash
JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
USER = wsaryada
GRADLE_HOME = /usr/local/gradle/gradle-2.10
HOME = /Users/wsaryada