Parameters are Passed by value or reference in Java

This is sometime confusing whether in java it is “passed by Value or Reference”. Here is the answer to this confusing question.

In Java parameters are “passed by value”.

  1. In case of Primitive Data Types, the data value of actual parameter is passed.
  2. If actual parameter is a reference to an Object (Class,Enum or Array object instance), then the reference value is passed, not the object itself. Means changes made to formal parameters will be reflected in the actual parameters.

ClassNotFoundException vs. NoClassDefFoundError java

This is very common interview question in core java. Here is the difference between the two. Both of them occur at run-time in application but they occur in different scenario.

  1. ClassNotFoundException:- ClassNotFoundException is a runtime exception which occurs when we try to load a class at runtime using Class.forName() method but dependent jar is not present in the classPath. Popular example for this could be when we connect to Database using JDBC we use Class.forName() to load the Driver Class to get the connection.

    if mysql jar is not present in the classPath then we get ClassNotFoundException.
  2.  NoClassDefFoundError:- NoClassDefFoundError is an error which occurs at runtime when class is available at compile time and but at runtime class is not available.

    Now if we compile the above given program then we get two class files A.class and B.class but if delete A.class then we will get NoClassDefFoundError.

Instance Member variable are overwritten not overridden java

Member variable are not overridden, they are overwritten.

  1. In subclass member variable to be available they need to be either protected/public.
  2. Instance member variable of subclass hide the variable of super class. e.g. parent.variable will call variable of parent and child.variable will call variable of child. See example given below.

    Example 2: