No, null = null is an invalid statement. It will not even compile.
NullPointerException is thrown whenever you try to access an instance field or call an instance method using a reference that is pointing to null.
About Question enthuware.ocajp.i.v7.2.1092 :
Moderator: admin
-
- Site Admin
- Posts: 10382
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
-
- Posts: 1
- Joined: Fri Sep 11, 2020 4:49 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1092 :
I think having a mental picture of a linked list actually helps to understand and follow the code in this question even for someone who has not had to work with linked lists. Thank you for mentioning it!admin wrote: ↑Wed Aug 01, 2012 11:02 pmThe Holder class is actually a very basic (and well known) building block of a linked list. It is usually named "Node" and not Holder. Please google linked list to learn more.
Though not important for this exam, understanding how linked lists work is something that a programmer must know. You mentioned that you've been programming for years and I am surprised to know that you didn't recognize it.
HTH,
Paul.
-
- Posts: 9
- Joined: Mon Jun 28, 2021 4:50 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
a.link = b does not mean a.link = b.link. It means a.link points to the same object to which b is pointing.
I imagined a and b - these are some country, link and value there are some lakes. a has own lakes and b has own lakes. How lakes of a can be assigned to another country but not to another lake? How is a.link=b works in reality? When I have saw it my brain was broken. How it happened in programming area? Please give me to understand this.
I imagined a and b - these are some country, link and value there are some lakes. a has own lakes and b has own lakes. How lakes of a can be assigned to another country but not to another lake? How is a.link=b works in reality? When I have saw it my brain was broken. How it happened in programming area? Please give me to understand this.
-
- Posts: 9
- Joined: Wed Jul 31, 2013 1:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Hi,
What I don't understand is why this code even compiles when non-static variable 'link' is being accessed inside a static context:
Shouldn't this violate that rule? Thank you.
What I don't understand is why this code even compiles when non-static variable 'link' is being accessed inside a static context:
Code: Select all
public static Holder setIt(final Holder x, final Holder y){
x.link = y.link; //<-non-static variables?
return x;
}
-
- Site Admin
- Posts: 10382
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
It is not being accessed from a static context. It is being accessed through variables x and y, which are pointing to instances of Holder class. The context of 'link' is the instance pointed to by x or y. If you try to access link without providing the context of an instance, then it would fail. Like this:
Just a suggestion - You should go through a good book to understand the theory before attempting the questions. We recommend this: https://amzn.to/4dEACui
Code: Select all
public static void staticMethod(){
System.out.println(link); //will not compile because here link is being accessed from a static context. static method does not have 'this'
}
public void instanceMethod(){
System.out.println(link); //will compile because here link is being accessed through the implicit instance variable 'this'
}
Who is online
Users browsing this forum: Bing [Bot] and 99 guests