Jump to content

problem with static declaration

- - - - -

  • Please log in to reply
1 reply to this topic

#1
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
class outside{
int a,b;
static void w(){
inner x=new inner();
x.y();
}
static class inner{
void y(){
System.out.println("Print");
}
}
}

class example43{
public static void main(String args[]){
outside r=new outside();
r.w();
}
}

in the above code if inner class is not made static then that results in an error when x is declared in w. why?

if the reason is that a static method may not call a non static method then another error should when x.y() is executed because y is not a static method. is the error not occurring because somehow making the inner class static has automatically made the method y static?

what is the meaning of making a class static?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
If that class was not static, it would be bound to an instance of the outer class. And since static and "instance of" don't really match, there's no way the static method is able to get that inner class.
Look at a non-static inner class like a big variable in a class.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users