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

How do you solve this...??!!

 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code...
public class Bill {
private static int i ;
private int j;
public static void staticMethod() {
System.out.println("static method in bill" );
}
interface INested { int Y2K = 2000; }
protected static class Nested implements INested {
private int k = Y2K;
private static int x ;
private void nonstatic() {
int ii = i;
staticMethod();
}
public static void main (String args[]) {
int ii = i;
Bll b = new Bll();
b.staticMethod();
}
}
}
*****when i run it*****
C:\SCJP>java Bill$Nested
static method in a bill
***********************
Perfect. Now is there a way to access the nonstaticmethod()?
in Nested class from its entry point main???
Any help please...?
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ragu,
You can't access nonstatic() using the object reference b since this method is not visible to class Bill. But you can create an instance of the static class and access the method through it.

Correct me if I am wrong,
Vanitha.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vanitha Sugumaran:
[B]Hi Ragu,
You can't access nonstatic() using the object reference b since this method is not visible to class Bill. But you can create an instance of the static class and access the method through it.

Correct me if I am wrong,
Vanitha.[/B]


thankx for your response
Yeah it will work, but the irony is
instance of a static class? hmmm
sounds little weird to me...
But i guess that seems to be the only way to access it
Probably the compiler just ignores for a moment the top-level nested class and handles it to be just an non-static inner class? (I dont know, but it looks like it though)
Just another X-files mysteries...
Thankx guys
and especially to Vanitha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic