• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Why this Output?

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




Output of this program is I am Weasel...
After callin g.doSomething(); not output is displayed for this call....
Why is it so?


[BSouther: Added UBB CODE tags]
[ March 05, 2008: Message edited by: Ben Souther ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's not for lack of spaces or semicolons.

There are two methods here called "doSomething," but these are entirely different methods.

In the class Test, doSomething() creates an instance of Inner. A reference to that instance is upcast to type GreaterOne and returned by the method. Nothing is output.

In the class Inner, doSomething() prints "I Am Weasel."
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still dont get it!



Line 1 : Instantiates a new Test object.

Line 2: Calls the doSomething() method which prints "I am a Weasel".

Line 3 : Does nothing!!!



In the class Test, doSomething() creates an instance of Inner. A reference to that instance is upcast to type GreaterOne and returned by the method. Nothing is output.


I dont get it!! Why is the output not executed?
Can somebody please explain?


Uttara Rishi
[ March 05, 2008: Message edited by: Uttara Rishi ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Uttara Rishi:
...Line 2: Calls the doSomething() method which prints "I am a Weasel".

Line 3 : Does nothing!!!...


No, line 2 does not result in any output. Line 2 calls doSomething in Test. That method contains a local class definition for Inner. But all the method does is create a new instance of Inner (based on the local definition) and return a reference.

Line 3 uses the reference to call doSomething in Inner, and that is where the output occurs.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, there are 2 separate doSomething() methods.

In Test, doSomething() is basically just...

In Inner, doSomething() is just...
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc .I got it!!
 
Swati Kadam
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By commenting out which line will the following class compile with no errors ?

class ZZY {

transient final int i = 0;
transient final static int j = 10 ;
int k = 78 ;

char jack(){
return 'a';
}

public static void main ( String args [ ]) { // ---> 1
new ZZY ( ) . jack ( ) ; // ---> line 2
int d = new ZZY ( ) . j + new ZZY ( ) . i ; // ---> line 3
new ZZY ( ) . k ; // ---> line 4
System.out.println(newZZY().i+" "+new ZZY().j);//--> line 5
}
}


In this program you have to comment line 4...
new zzy().K; In this line new ZZY() returns object of class zzy and you can access any instance variable as object.varname...
but here why we cant access it?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Swati Kadam:
...you can access any instance variable as object.varname...
but here why we cant access it?


The reference is fine. The problem is that you're not doing anything with that line, so it's not a statement. If you use this reference in a complete statement, then it's fine. For example...

int x = new ZZY().k;
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically what you meant to say was that you cannot access a mehtod local inner class's methods from outside that method where its defined. am i right marc?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic