Method Parameter Passing: by Value vs by Reference

Pass by value: The local parameters are copies of the original arguments passed in. Changes made in the function to these variables do not affect originals. 

Pass by reference: The local parameters are references to the storage locations of the original arguments passed in. Changes to these variables in the function will affect the originals. No copy is made, so overhead of copying (time, storage) is saved.

The Java Spec says that everything in java is pass-by-value. There is no such thing as “pass-by-reference” in java.

The key to understanding this is that something like

Dog myDog;

is not a Dog; it’s actually a pointer to a Dog.

What that means, is when you have

Dog myDog = new Dog("Rover");

you’re essentially passing the address of the created Dog object to the foo method.

(I say essentially because Java pointers aren’t direct addresses, but it’s easiest to think of them that way)

Suppose the Dog object resides at memory address 42. This means we pass 42 to the method.

if the Method were defined as

public void foo(Dog someDog) {
    someDog.setName("Max");     // AAA
    someDog = new Dog("Fifi");  // BBB
    someDog.setName("Rowlf");   // CCC

let’s look at what’s happening.

  • the parameter someDog is set to the value 42
  • at line “AAA”
    • someDog is followed to the Dog it points to (the Dog object at address 42)
    • that Dog (the one at address 42) is asked to change his name to Max
  • at line “BBB”
    • a new Dog is created. Let’s say he’s at address 74
    • we assign the parameter someDog to 74
  • at line “CCC”
    • someDog is followed to the Dog it points to (the Dog object at address 74)
    • that Dog (the one at address 74) is asked to change his name to Rowlf
  • then, we return

Now let’s think about what happens outside the method:

Did myDog change?

There’s the key.

Keeping in mind that myDog is a pointer, and not an actual Dog, the answer is NO. myDog still has the value 42; it’s still pointing to the original Dog (but note that because of line “AAA”, its name is now “Max” – still the same Dog; myDog‘s value has not changed.)

It’s perfectly valid to follow an address and change what’s at the end of it; that does not change the variable, however,

Java works exactly like C. You can assign a pointer, pass the pointer to a method, follow the pointer in the method and change the data that was pointed to. However, you cannot change where that pointer points.

In C++, Ada, Pascal and other languages that support pass-by-reference, you can actually change the variable that was passed.

If Java had pass-by-reference semantics, the foo method we defined above would have changed where myDog was pointing when it assigned someDog on line BBB.

Think of reference parameters as being aliases for the variable passed in. When that alias is assigned, so is the variable that was passed in.

Copy Rights @


Keywords static and final in Java

static means the variable/method belongs to the class not an instance, so there is only one copy of this data shared between instances.
final is a method to define the variable/method having only one initialization, either in initializing or within a constructor.
A note on final methods and final classes, this is a way of explicitly stating that the method or class can not be overridden / extended respectively.

static variable:
1. A variable which belongs to the class and not to object (instance).
2. It is initialized only once at the start of the execution, before the initialization of any instance variables.
3. Syntax: Class.variable (no need to new an object)

static method:
1. It belongs to the class and not the object (instance)
2. A static method can access only static data unless it uses an instance of some class to indirectly access the instance variable.
3. A static method can call only other static methods  unless it uses an instance of some class to indirectly access the instance method.
4. Syntax: Class.methodName() (no refer to this or super keywords)

static class:
There is a static nested class in java. When you declare a nested class static, it automatically becomes a stand alone class which can be instantiated without having to instantiate the outer class it belongs to.

public class A
 public static class B

Because class B is declared static you can explicitly instantiate as:

B b = new B();

Note if class B wasn’t declared static to make it stand alone, an instance object call would’ve looked like this:

A a= new A();
B b = B();

static block codes:
It’s a static initializer. It’s executed when the class is loaded (or initialized).

static {
        private static Name = "blog";

final keyword
A final class cannot be subclassed.  All methods in a final class are implicitly final. This is done for security and efficiency reasons.

A final method can’t be overridden by subclasses. This is  used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.

A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a blank final variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error will occur.

public class Shirt {
static final String color = "Black";
public class Test {

public static void main(String args[]) {
    System.out.println(Shirt.color); //should provide output as "Black"
            Shirt.color = "white"; //Error because the value of color is fixed.  
    System.out.println(Shirt.color); //Code won't execute.

final static String a = “test” or static final String a = “test” ? 
They are the same as order in keyword doesn’t affect the functionality.