Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What's the result of this inner class code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What Watch "What New topic
Author

What's the result of this inner class code?

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Hello folks.

What is the result of this code?
ps: try without run the code, of course.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

NullPointerException?? (I didn't run it as you said but NPE is what I think will be the output). Is this just a quiz for fun or are you confused on the output??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Ankit Garg wrote:NullPointerException?? (I didn't run it as you said but NPE is what I think will be the output). Is this just a quiz for fun or are you confused on the output??

The output is: nullA B

I thought the result is very interesting. Initially I thought the output would be B
But after analyze, I could understand.

Valentin Ivanov
Ranch Hand

Joined: Nov 20, 2008
Posts: 38

I thought there is a compile error because of the declaration in main method. Shouldn't it be
instead of
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Valentin Ivanov wrote:I thought there is a compile error because of the declaration in main method. Shouldn't it be
instead of
.

No. This syntax is used to access inner classes that are not visible to the class.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Leandro,

This is definitely an interesting thing but there is one more specific thing with String that here + operator and contact does not always give me the same result as mentioned in the below code -


Just think about the possible out put and reason of java behaving in such a way. I could get an explanation of every thing except how null +null becomes nullnull.


Any thoughts why null+null does not give NPE ?


Regards
Salil Verma
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Salil Vverma wrote:Hey Leandro,

This is definitely an interesting thing but there is one more specific thing with String that here + operator and contact does not always give me the same result as mentioned in the below code -


Just think about the possible out put and reason of java behaving in such a way. I could get an explanation of every thing except how null +null becomes nullnull.

Here is the reason: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#valueOf(java.lang.Object)
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Leandro,

If we focus on below section of code abc is an object which is null, so while using + operator should not jvm call toString() function directly on abc rather than valueOf() ? What is your opinion on this ?


Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Got the point.

Actually + operator performs the concatenation by using append function of StringBuilder class, which calls the value of method in the first operand. append(String) function in StringBuilder internally performs append operation by calling append method of AbstractStringBuilder class which checks for the second operand if it is null, then it is take its value as "null" string.

In this way, it return nullnull as a result. In short the the code converts to following code after compilation -



Thanks for giving the hint of valueOf(Object) .
Valentin Ivanov
Ranch Hand

Joined: Nov 20, 2008
Posts: 38

Leandro Coutinho wrote:No. This syntax is used to access inner classes that are not visible to the class.

I tried and it works as you says but K&S book on Chapter 8, page 667 says:
1. The code to make an instance from anywhere outside nonstatic code of the outer class

The preceding code is the same regardless of whether the main() method is within MyOuter class or some other class.

On page 668 /next page/ says:
2. From outside the outer class instance code (including static method code within the outer class) , the inner class name must now include the outer class's name: MyOuter.MyInner


I'm confused about this. Could you please clear my doubts?
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Valentin ,
With in the same class you can access the inner classes with name of inner class or with the name outer.inner class both of them are fine. But from another class you shall have to use outer.inner format only then the inner class would be accessable.

As in the below code. test2 function in StaticVariableAndInhiretance class would give compilation error reason being it does not what B is. test1 and test2 functions in StaticVariableAndInhiretance1 class woks fine for the same reason as mentioned above.

I hope this would clear all your doubts.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What's the result of this inner class code?
 
Similar Threads
Nested classes - Series of doubts
inner class
a simple brainer on inner classes
creating objects in java
Static Block