File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Doubt regarding extending a class and calling inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding extending a class and calling inner classes" Watch "Doubt regarding extending a class and calling inner classes" New topic
Author

Doubt regarding extending a class and calling inner classes

Ashwin Soni
Greenhorn

Joined: Jun 22, 2012
Posts: 18

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Jun 22, 2012
Posts: 18

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

Joined: Jun 22, 2012
Posts: 18

Proof of compile.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Do you want me to post proof of the compiler error, using JDK7u9?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ashwin Soni
Greenhorn

Joined: Jun 22, 2012
Posts: 18

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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.)


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding extending a class and calling inner classes