Here’s another little tutorial that most amateur developers need to take the time to read through. When it comes to objects and pointers in Java people seem to get confused without the explicit *’s and &’s that most C-based languages offer. New developers seem to often lose track of whats a pointer and whats a value when programming in Java and therefore end up with lot’s of unexplained behavior/bugs or exceptions in their code.
Below is a series of tests which demonstrates most (if not all) the scenarios where pointer confusion takes place. The following should at least solidify this basic concept for Java programmers to a degree of comfort that pointer confusion is no longer an issue. Enjoy:
Here is a class, Something, that is composed of another class, SomethingElse.
Something.java:
public class Something { private SomethingElse somethingelse; public SomethingElse getSomethingelse() { return somethingelse; } public void setSomethingelse(SomethingElse somethingelse) { this.somethingelse = somethingelse; } public void print() { somethingelse.print(); } }
Here is the SomethingElse class that Something is composed of:
SomethingElse.java:
class SomethingElse { public int n = 0; public void print() { System.out.println(n); } }
Finally, here is the Main class which contains out main() method with all our tests. The tests purposes are identified in the comments:
Main.java:
public class Main { public static void main(String[] args) { SomethingElse a = new SomethingElse(); SomethingElse b = new SomethingElse(); SomethingElse c = new SomethingElse(); a.n = 1; b.n = 2; c.n = 3; Something something = new Something(); something.setSomethingelse(a); System.out.print("Value of something before any modifications to a: "); something.print(); /* * Let's try assigning a new value to a in the Main class to see if it * affects the value held within the something object. */ System.out.println("==Test 1=="); System.out.print("Value of a before modification: "); a.print(); a.n = 100; System.out.print("Value of a after modification: "); a.print(); System.out.print("Value of something after first modification of a: "); something.print(); /* * Let's try assigning the pointer in b to the pointer in a then changing b and printing out a. */ System.out.println("==Test 2=="); System.out.print("Value of a before modification: "); a.print(); b = a; b.n = 25; System.out.print("Value of a after modification of b: "); a.print(); System.out.print("Value of something after first modification of b: "); something.print(); /* * Let's try assigning the pointer in a to the pointer in c. */ System.out.println("==Test 3=="); System.out.print("Value of a before modification: "); a.print(); a = c; System.out.print("Value of a after modification: "); a.print(); System.out.print("Value of something after second modification of a: "); something.print(); /* * Let's try setting the pointer in a to null. */ System.out.println("==Test 4=="); System.out.print("Value of a before modification: "); a.print(); a = null; System.out.println("Attempting to print a after nullifying it's pointer will throw a NullPointerException so let's move on."); System.out.print("Value of something after third modification of a: "); something.print(); } }
Output:
Value of something before any modifications to a: 1 ==Test 1== Value of a before modification: 1 Value of a after modification: 100 Value of something after first modification of a: 100 ==Test 2== Value of a before modification: 100 Value of a after modification of b: 25 Value of something after first modification of b: 25 ==Test 3== Value of a before modification: 25 Value of a after modification: 3 Value of something after second modification of a: 25 ==Test 4== Value of a before modification: 3 Attempting to print a after nullifying it's pointer will throw a NullPointerException so let's move on. Value of something after third modification of a: 25
So to explain what’s happening here.
In the first test we’re testing to see whether changing the value of a in the Main class will affect the value within something’s somethingelse class because it holds a pointer to the same memory space that a does. The test does change the value of a both within the Main class and the value of n within something’s somethingelse class.
In the second test were attempting to show how you can assign object pointers and alter the same memory space through different object pointers. We’re doing this by assigning b the same pointer held by a and then changing b and printing out the value of a which prints out the new value that we just assigned b. This happens because when we assign b the pointer held by a we have assigned both a and b to point to the same memory space. When we alter b we are altering the data in that memory space and when we go to look at it through a we are still looking at the same memory space we just altered with b. Get it? Got it? Good.
The third test is to show the opposite of test two where something’s somethingelse class holds a reference to the memory space held by Mains a object pointer but then we assign a the same pointer held by c thereby losing our pointer from a to the same memory location as something’s somethingelse class. This test is to show that assignment of objects with the ‘=’ operator only works on pointers so after we do this something’s somethingelse class doesn’t take on the value of c we just effectively eliminate our pointer through a to the same memory space held by something’s somethingelse class.
The last test is just to reinforce the same concept the previous three were trying to demonstrate. We eliminate the pointer held in a so if we try to access it the code would throw a NullPointerException in the Main class but something’s somethingelse class still holds the reference to the original memory space and the value printed remains the same.
I hope this demonstrates enough examples of object pointers to clear up some of the confusion. There are a number of other things I could have shown in this code but they would have been just as redundant as the fourth test in the code already provided.
Keep coding!


