• Post Reply Bookmark Topic Watch Topic
  • New Topic

javap makes it even more worse  RSS feed

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That what I got when I use javap javap with my Anonymous class (Anon2$1)
Which I made inside concrete class(Anon2) ,Anon2$1 also implements interface My which has abstract show ()method inside it
I am not sure why the following lines are included at compile time
:
final Anon2 this$0;
Anon2$1(Anon2);
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:
I am not sure why the following lines are included at compile time
:
final Anon2 this$0;
Anon2$1(Anon2);


What you are seeing are implementation details for inner classes. Inner classes need to access its outer class. Instantiating an inner class needs an instance of the outer class.

Don't worry. You won't directly use those reference(s) or the constructor(s) -- these will be translated internally for you... when you instantiate an inner class or when your inner class access its outer class.

Henry
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will you please elaborate by example code as these are only prototype so how they are used is not clear (I am pretty much curious to know)
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you show us the code you created that .class file from?
Remember: if you miss out the -c option for javap, you only get headers of methods, constructors, field names, etc. You don't get the full bytecode.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason behind not posting the code is I am on very slow mobile connection so it takes a decade to open desktop version of this site
Which I'll need to use code tags and all
Hope you'll understand the problem
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:Will you please elaborate by example code as these are only prototype so how they are used is not clear (I am pretty much curious to know)


Well, it is going to be hard providing an example, since it is an implementation detail, which the compiler hides from you. Also, you are using anonymous inner classes, whose outer class is harder to envision... but how about this? And you can do it yourself.

First, make sure that your code for the anonymous inner class is in an instance method. Second, create an instance variable in your class (that had the method that creates the inner class). If you haven't figured it out, this class is your outer class. Third, modify your inner class to use the instance variable -- as if it was simply in scope.

Notice that the compiler doesn't complain? Why? Well, because the inner class can access instance variables of its outer class.

But how it is doing it? How is it accessing an instance variable that is in another class/instance? Well, obviously, it has an internal reference to it... which you have seen when you examined the byte code.

Henry
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
What a matter of coincidence same thing came in my mind while I was using Normal inner class that what forced me to inspect the byte code
Then I found javap tool
I thought of doing same with 2 real classes because I believed (before this)what compiler can do
I can do it too explicitly
So I made a Base class containing instance method and created an instance of Base class and one other class Child in which I passed reference id of Base class to its constructor
In Child class (just used class names randomly they are not related only Base class has object of Child)
Now in Child class I used instance variable as of Base type and through constructor initialized it
With reference id of Base class
And also made a default constructor
Now in main method I created object of Child class with default constructor then tried of accessing instance method of Base class


Basically I was trying to call instance method of Base class in Child class without creating object of Base class in Child class

Able to compile it but can't run and Nullpointer exception occurred
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!