Homebrew installs java at /usr/local/Cellar/openjdk/. There may be a requirement where you may need to run multiple version of java on the same MacOS. It involves few steps that we need to follow to setup and configure the needed version on our machines.

Find all related java version
$ brew search java

To show all java version details
$ brew info java

To install the latest version of java
$ brew install java

To list the version of java
$ java -version

When java is installed, it is installed as keg-only ( if it is not linked into the Homebrew prefix ). To setup java to path variable, we will need to add a symbolic link. To list the jdk file installed, issue

$ ls -lsa /usr/local/Cellar/openjdk/

Creating a symbolic link to the new version installed

$ sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

To list the version of java
$ java -version

To install a very specific java version
$ brew install openjdk@11

Openjdk@11 is keg-only and we will need to setup a symbolic link

Creating a symbolic link to the new version installed
$ sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

To list all the versions that are installed
$ ls -lsah /Library/Java/JavaVirtualMachines/

As a new version of java is installed to be the default version, we will need to update ~/.zshrc for the PATH settings

$ echo ‘export PATH=”/Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home/bin:$PATH”‘ >> ~/.zshrc

To list and check the version of java installed
$ java -version