• Post Reply Bookmark Topic Watch Topic
  • New Topic

An interesting question  RSS feed

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1] If the following code is compiled, what will the output be? (Don't cheat! I made this one up!)
<pre>
import java.io.*;
import java.awt.event.*;
public class test {
public test() {
new inner();
class inner {
inner() {
System.out.println("yer runnin' me!");
}
}
}
public static void main(String arg[]) {
new test();
}
}

class inner {
inner() {
System.out.println("how strange!");
}
}
</pre>
A) how strange!
B) yer runnin' me!
C) Compiler Error, because you cannot have duplicate class names
D) Runtime Exeption, because duplicate class names detected.
Yours,
Khalid


<pre>
ps: Compile it, find out what the answer is... try switching around the order of the constructor...
</pre>
[This message has been edited by Khalid Bou-Rabee (edited July 17, 2000).]
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it could be (A) as the local inner class is not in the
scope for new inner();
Correct/Wrong?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there:
I tried it on my compiler and tried to change the position of constructor, but can you explain why?
Thanx
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I believe you can only instantiate the non-static inner class with the instance of the outter class which was not found in the constructor where only contains " new inner();", therefore, the top level inner class is invoked.
thanks in advance for your correction.
tian
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Because we have only one class it's name is Inner(top-level), for nested class it's name is Test.Inner. I change the code , this time compiler will constructs nested Inner, not top-level Inner. Try this:
public class Test {
public Test() {
new Inner();
}
class Inner {
Inner() {
System.out.println("yer runin' me!");
}
}
public static void main(String[] args) {
new Test();
new Test().new Inner();
}
}
class Inner {
Inner() {
System.out.println("how strange!");
}
}
The answer will be:
"yer runin' me!"
"yer runin' me!"
"yer runin' me!"
Oh, it's wonderful!
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, look at the following code : compile and run it - You will get the idea
import java.io.*;
import java.awt.event.*;
class Test {
Test() {
class inner{
inner(){
System.out.println("Hidden Inner");
}
}
new inner();
}
public static void main(String args[]){
Test x = new Test();
}
}
class inner {
inner() {
System.out.println("Visible Inner");
}
}

[This message has been edited by jafarali (edited July 17, 2000).]
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain the logic behind this behavior
why the top level class Inner's constructor is not invoked ?
Any java wizards to clarify it ?
Thanx in advance

Originally posted by kevin jia:
Hi,
Because we have only one class it's name is Inner(top-level), for nested class it's name is Test.Inner. I change the code , this time compiler will constructs nested Inner, not top-level Inner. Try this:
public class Test {
public Test() {
new Inner();
}
class Inner {
Inner() {
System.out.println("yer runin' me!");
}
}
public static void main(String[] args) {
new Test();
new Test().new Inner();
}
}
class Inner {
Inner() {
System.out.println("how strange!");
}
}
The answer will be:
"yer runin' me!"
"yer runin' me!"
"yer runin' me!"
Oh, it's wonderful!


 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any one pls explain the logic behind it
thanx in advance

Originally posted by Khalid Bou-Rabee:
[b]1] If the following code is compiled, what will the output be? (Don't cheat! I made this one up!)
<pre>
import java.io.*;
import java.awt.event.*;
public class test {
public test() {
new inner();
class inner {
inner() {
System.out.println("yer runnin' me!");
}
}
}
public static void main(String arg[]) {
new test();
}
}

class inner {
inner() {
System.out.println("how strange!");
}
}
</pre>
A) how strange!
B) yer runnin' me!
C) Compiler Error, because you cannot have duplicate class names
D) Runtime Exeption, because duplicate class names detected.
Yours,
Khalid



<pre>
ps: Compile it, find out what the answer is... try switching around the order of the constructor...
</pre>
[This message has been edited by Khalid Bou-Rabee (edited July 17, 2000).][/B]


 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is, that its the following rule gives us the answer:
"The scope of a local class declared in a block is the rest of the immediately enclosing block, including its own class declaration." ( JLS 2 sec 6.3 )
its not different from any local variable decleration. local variables are visible in the enclosing block, FROM THE POINT they are declared"
this slight addition to the same pgm illustrates the point. note that the behaviour is the same for both the variable s and the class instantiation. if some local identifier is present and in scope, it will hide the others, else others wud be considered.
---
public class testinner
{
String s = "class string";
public testinner()
{
System.out.println( s) ;
String s = "method string";
System.out.println( s) ;
new inner();
class inner
{
inner()
{
System.out.println("yer runnin' me!");
}
}
new inner();
}
public static void main(String arg[])
{
new testinner();
}
}
class inner
{
inner()
{
System.out.println("how strange!");
}
}
----
cheers,
Vivek
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I first read the question and concluded the ans to be
"how strange" then compiled and found to be correct.
Following was my thought process
The inner class "inner" has no independant identity of it own
ie it has to have a outer class handle in order to be constructed hence on a new inner()inside the test constructor outer "inner" class is called
to construct inner "inner" class we may have to do
new test().new inner();
Now in order to prove it I tried to construct an object in the
main() method as new test().new inner(); but was unable to do so.
Can anybody help!!
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really pretty simple. As another person already mentioned, it's a matter of code placement. You have to declare the class before you can use it!
import java.io.*;
import java.awt.event.*;
public class test {
public test() {
new inner(); //at this point, the local class inner //(declared below) can't be used, but the compiler finds the //top-level version and uses that. If you ommit that top-level //version of class inner, you'll get a compile-time error saying //that the compiler doesn't know anything about a class inner.
class inner {
inner() {
System.out.println("yer runnin' me!");
}
}
//inner(); //Ok, the above class inner exits and takes precedence
//over the top-level version which is in a different scope!
}
public static void main(String arg[]) {
new test();
}
}
class inner {
inner() {
System.out.println("how strange!");
}
}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to add that this behavior (i.e., declaration before first use) is for local classes. Obviously, that top-level version of inner is used before it's declared. I think you're also safe w/ non-local inner classes, too.
 
Khalid Bou-Rabee
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor perfroms in a top-down fashion. So the inner-class in the method is not instantiated until ~after~ you run the constructor of the outer class.
Now, if you put the constructor call after the initialization of the inner-class you'll call the inner-class, and not the outer one.
I'm off to take my exam in 30 minutes!!! I'm SHAKING!!!
lol,
Khalid
 
Khalid Bou-Rabee
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is A
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!