• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which variables class can access....  RSS feed

 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For A Class Defined Inside A Method, What Rule Governs Access To
Variables Of Enclosing Methods ?

(a) The Class Can Access Any Variable.
(b) The Class Can Only Access Static Variables.
(c) The Class Can Only Access Transient Variables.
(d) The Class Can Only Access Final Variables.

Plz Let Me Know The Answer..As Quickly As You Can...Its Urgent..
Thanx in Advance...
Prash
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you have a compiler to answer you this really tough question?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java in General (Beginner.)

And please, folks, it's obviously a test question -- make him answer it, but please don't just give him the answer.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh, I used to know this for the SCJP exam. Haven't used many method local inner classes (or whatever they're called) since then, so would need to google it or open a study guide to find out the answer....
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol never heard of defining a class inside a method before.

how does that work?

you wouldn't get an illegal start of expression?

-Justin-

ima noob
 
Tom Fulton
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Justin, it's a fairly common technique called an Anonymous Inner Class. It's called "anonymous" because it is used only once, at the point of instantiation, and consequently does not need a class name (because it will never be re-used. One place you see people do this is when they want to sort a Collection of objects on a particular attribute. The Collections.sort( ) method takes the collection to sort, and the comparator used for the sorting. If the comparator is only used for this kind of sort once, many developers will define the comparator class right inside the sort method.

Personally, although I can read this kind of code and even write it on occasion, I find it overly cryptic and nasty. Writing as a regular (i.e. with a name) inner class is more readable for me.

To address the original question: there are 3 different forms of inner classes: regular (instance), static and anonymous. Based on the names, you can probably figure out what parts of the enclosing class they can access. And if you can't, 5 minutes with a Java compiler will answer the question.

It may be an emergency to you, but most of us are more concerned with getting you to learn the concept rather than get the answer right.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom, I'm pretty sure you missed one of the inner class types. This is the Inner Class that is defined inside a method, but has a name (method local inner class, or just local inner class, I'm not sure).



Yuriy
 
Tom Fulton
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's interesting...I never knew you could do that. But I wonder about its applicability. In the example you gave:


the class "Bar" contributes nothing "classlike" to the method. Essentially there is no reason to define it with a name if it's only going to be used one time. I guess it makes sense, in that it doesn't technically have to be anonymous, but it would more typically be written that way.

My preference is not to define classes within a method, or a method call...I find it less than intuitive to many developers. For example, if I am implementing a Comparator used to sort by Social Security Number, I will give it a name like SSNComparator. Although it adds an identifier to the namespace (or at least the package namespace), it acts as good documentation.

Thanks for teaching me a feature of Java I never knew before!
[ May 04, 2006: Message edited by: Tom Fulton ]
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike anonymous classes, with this kind of definition you can create multiple instances of the class. I've actually only used these types of classes as an ugly ugly hack in a few JSP pages. In JSP you cannot use template output inside declared methods, so my workaround was to create an inner class in the JSP page that would be counted as a part of the main service method. That way the methods of that class could use template output.

Yuriy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!