• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's the result of this inner class code?

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello folks.

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

 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought there is a compile error because of the declaration in main method. Shouldn't it be
instead of
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic