The Artima Developer Community
Sponsored Link

Java Answers Forum
Question On Nesting A Class Within A Method

4 replies on 1 page. Most recent reply: Mar 9, 2004 5:50 AM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Kevin

Posts: 25
Nickname: doublek321
Registered: Jan, 2004

Question On Nesting A Class Within A Method Posted: Mar 8, 2004 6:21 PM
Reply to this message Reply
Advertisement
My question relates to the code below.

First off, what is the proper way to refer to the class "NestedClass"? Is it a local inner class? Is it "inner" because it is within another class and "local" because it falls out of scope at all times other than when inside the method?

Secondly (and I think I know the answer to this but want confirmation), is there any way to call the method "anotherWayToPrintPrivString"? I would guess the answer is "no" because I'm thinking you'd have to downcast to call it like this...

((NestedClass)mi).printIt();


Is the reason you can't downcast this because of the "local" part of "local inner class"?


interface MyInterface {
	void printPrivString();
}
 
 
class NestClassWithinMethod {
	MyInterface methodWithNestedClass (String theString) {
		class NestedClass implements MyInterface {
			private String privString;
 
			NestedClass(String abc) {
				privString = abc;
			}
			
			public void printPrivString() {
				System.out.println("NestedClass.printPrivString privString is " + privString);
			}
			
			public void anotherWayToPrintPrivString() {
				System.out.println("privString = " + privString);
			}
		}
		
		return new NestedClass(theString);
	}
	
	public static void main (String args[]) {
		NestClassWithinMethod ncwm = new NestClassWithinMethod();
		MyInterface mi = ncwm.methodWithNestedClass("how now brown cow");
		mi.printPrivString();
		//((NestedClass)mi).printIt();
	}
}
 


Kevin

Posts: 25
Nickname: doublek321
Registered: Jan, 2004

Re: Question On Nesting A Class Within A Method Posted: Mar 8, 2004 6:26 PM
Reply to this message Reply
> Secondly (and I think I know the answer to this but want
> confirmation), is there any way to call the method
> "anotherWayToPrintPrivString"?


Actually the end of the question should read...

is there any way to call the method "anotherWayToPrintPrivString" from main?

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Question On Nesting A Class Within A Method Posted: Mar 8, 2004 8:47 PM
Reply to this message Reply
Not without casting. This goes back to what I was explaining about interfaces in one of the other posts. Since you have mi declared as a MyInterface object, the compiler only "knows" about the printPrivString() method since that is the only one guaranteed to be there by the interface.

If you either declare it or typecast it to a NestedClass object, then the compiler can read the NestedClass file to see what other methods a NestedClass object contains. (Disclaimer: I've never seen a class declared in a method like this, so I don't even know if you *can* declare or typecast it.)

twc

Kevin

Posts: 25
Nickname: doublek321
Registered: Jan, 2004

Re: Question On Nesting A Class Within A Method Posted: Mar 8, 2004 9:08 PM
Reply to this message Reply
> If you either declare it or typecast it to a NestedClass
> object, then the compiler can read the NestedClass file to
> see what other methods a NestedClass object contains.

I'm wondering if there is any way to downcast it though. The class is defined ONLY within the method.

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Question On Nesting A Class Within A Method Posted: Mar 9, 2004 5:50 AM
Reply to this message Reply
> > If you either declare it or typecast it to a
> NestedClass
> > object, then the compiler can read the NestedClass file
> to
> > see what other methods a NestedClass object contains.
>
> I'm wondering if there is any way to downcast it though.
> The class is defined ONLY within the method.

Try it. As I said before, I've never seen code done like this, so you're getting out of my league. Sorry.
twc

Flat View: This topic has 4 replies on 1 page
Topic: Polymorphism Questions Previous Topic   Next Topic Topic: Re-stated More Clearly: Anononymous Local Inner Class Question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use