• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doesn't this class compiles?

 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The class Person is inside Lab1 class.In Lab1 a static variable pers of type Person is declared and used inside the main().Even though pers is of static type why is not possible to compile Lab1.If Person class is made static Lab1 compiles and returns the expected output.Since pers is inside main() it is of type static and it will refer to what ever exact object it will be pointed to.

Varuna
[ December 11, 2008: Message edited by: Varuna Seneviratna ]
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I changed the way the of the object creation of the method-local inner class and it works fine.
I hope this explains what you are looking for.

Thank you.
 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sudipto Shekhar:


I changed the way the of the object creation of the method-local inner class and it works fine.
I hope this explains what you are looking for.

Thank you.


So when referring to a class it's full path must be specified? Is it?

Regards Varuna
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
The class you created inside the main() is a method local inner class.
There is a special case with the method local inner classes.I recommend you to go through the static inner class, method local inner class and the anonymous inner and argument-defined anonymous inner class in deep.
They are very important concepts of Java and are very helpful in certain cases.

Hope this helps.
 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sudipto Shekhar:


I changed the way the of the object creation of the method-local inner class and it works fine.
I hope this explains what you are looking for.

Thank you.

[ December 11, 2008: Message edited by: Varuna Seneviratna ]
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry I did not get you!
 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sudipto Shekhar:
I am sorry I did not get you!


You were correct I did not change the tester() method when compiling the program in my machine that's why the theory you said did not work in my machine


Varuna
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you actually intend to have the Person class inside the Lab1 class? That appears very strange to me.
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Inner classes are the helper class to the outer class in most of the cases.

The best example is of event handling.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume this is an exercise; if you do want inner classes, this question is too difficult for beginner's. Moving.

******************************************************************************

Do a Google search for Bruce Eckel's Thinking in Java; the 3rd edition used to be available as an HTML and it has a nice chapter about inner and nested classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic