Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding extending a class and calling inner classes

 
Ashwin Soni
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI!




In above code I have called inner class a which is calling third inner class c. As per class c should call its constructor and class c extends b and class b extends class a. but nothing happens only "Calling class a" called. I am confused how and why this code working like this.

Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, that is some brilliant design. A local class in the constructor
Why did you say the code works and prints part of what you expected? The code you posted doesn’t compile.
By the way: you cannot call a class. You can only call a method or constructor or similar.
 
Ashwin Soni
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JDK 7 code compiles.

I am calling class constructor a it is giving a output by

I really want to know about how this code works.

Thanks.
 
Ashwin Soni
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Proof of compile.

 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want me to post proof of the compiler error, using JDK7u9?
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Soni wrote:I am confused how and why this code working like this.

You're not the only one.

All arguments about compiling aside, your names are atrocious (and non-standard), which makes it very difficult for anyone to read your code at all.
Secondly, I'm surprised that:
new c();
actually works at all, since I would have expected it to be:
new a.b.c();
but maybe the compiler is being nice because it's still the holidays.

My suggestion: come up with a scenario that actually makes sense, and code it using proper names. This kind of "what happens if I do this" exercise is rarely profitable.

Winston
 
Ashwin Soni
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Ashwin Soni wrote:I am confused how and why this code working like this.

You're not the only one.

All arguments about compiling aside, your names are atrocious (and non-standard), which makes it very difficult for anyone to read your code at all.
Secondly, I'm surprised that:
new c();
actually works at all, since I would have expected it to be:
new a.c();
but maybe the compiler is being nice because it's still the holidays.

My suggestion: come up with a scenario that actually makes sense, and code it using proper names. This kind of "what happens if I do this" exercise is rarely profitable.

Winston



However this code is compiling without errors.
When I call class A's constructor its constructor called and execute the second statement of out.println("Calling class a"); , but what happens with 1st statement of constructor "new c()" ?
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Soni wrote:However this code is compiling without errors.

You're not listening. Just because a program compiles doesn't mean that it either
(a) works, or
(b) teaches you anything
and from what I can see, yours does neither.

When I call class A's constructor its constructor called and execute the second statement of out.println("Calling class a")...

Well, all I can say then (admittedly from only a cursory glance) is that the code you've posted is not what you're running because, as far as I can tell, what you posted will throw a StackOverflowError before it ever gets to the second statement.

Come back when you have a properly written program that actually makes sense, and I'll try and help; otherwise I'll leave you to work things out for yourself.

Winston
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must have another class on your classpath named c because the code you posted doesn't compile without it (and it would explain the output you are getting as well.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic