• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's the result of this inner class code?

 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks.

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

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic