File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Have a class within a classs, how can I call setMethod from inner class from a separate class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Have a class within a classs, how can I call setMethod from inner class from a separate class?" Watch "Have a class within a classs, how can I call setMethod from inner class from a separate class?" New topic
Author

Have a class within a classs, how can I call setMethod from inner class from a separate class?

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Lets say I have a class like this, that also has another class within it:




Then I have a separate class called App. How do I call the setDeviceId method from the MeasurementProject.ProjectDevice() class? Here is some code to illustrate:



So in this class I need to set MeasurementProect.ProjectDevice.deviceId using its set method, but I have been having trouble figuring out how. Ive tried quite a few things but nothing seems to be working.


True wisdom is in knowing you know nothing - Socrates
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Removed incorrect assertion

John.
David Sharpe
Ranch Hand

Joined: Jun 15, 2009
Posts: 32
"class ProjectDevice" is the definition or blueprint for a "ProjectDevice" object. Until you actually declare a "ProjectDevice" object, you can't use its methods (basically).

I'm not positive what your intent is, but I bet the "MeasurementProject" class will have a "private ProjectDevice projectDevice = new ProjectDevice();" field, or something like that.

edit: Nested Classes (The Java Tutorials). Note the example on the next page.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I mean is there not something like

MeasurementProject.ProjectDevice project = new MeasurementProject.ProjectDevice;
project.setDeviceId():

I could use? That doesnt work by the way, i tried
David Sharpe
Ranch Hand

Joined: Jun 15, 2009
Posts: 32
Did you check out the link I posted?

Java Tutorial wrote:To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();


I'd say this is an unusual approach, but hey, it's your code.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's only if the inner class isn't static, though.

That aside, consider things like Map.Entry (an interface, not a class, but similar idea). It's not that unusual when the situation warrants it, although I rarely encounter non-static inners.

Edit: I just learned that static inner classes are called "nested" classes, not inner classes--so take what I said with a grain of salt :)
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 272

Matt Kohanek wrote:I mean is there not something like

MeasurementProject.ProjectDevice project = new MeasurementProject.ProjectDevice();
project.setDeviceId():

I could use? That doesnt work by the way, i tried


If you made your MeasurementProject.ProjectDevice class static then the above code would work. However, the ProjectDevice class would NOT have access to the non-static members of the MeasurementProject class (such as "name" and "permissions").


SCJP 6 || SCWCD 5
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Yeah I know it would be easier to just have the ProjectDevice() as its own class. But I was trying an experiment. But it is taking too long to get anything out of it so Im moving on anyhow.
Thanks
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Java Tutorial wrote:To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Unless you've got an inner class declared in a static context. Which is not the same thing as an inner class. Ye gods, what a mess Sun has made of their nomenclature.
David Sharpe
Ranch Hand

Joined: Jun 15, 2009
Posts: 32
Matt Kohanek wrote:Yeah I know it would be easier to just have the ProjectDevice() as its own class. But I was trying an experiment. But it is taking too long to get anything out of it so Im moving on anyhow.
Thanks


Yes, there are (or were, if you've given up on the "experiment") simpler methods. I'm not suggesting the following code is what you were looking for; I'm suggesting it's a more conventional (?) approach.


You seem to be toying with some pretty advanced syntax, e.g. MeasurementProject.ProjectDevice project = new MeasurementProject.ProjectDevice; without really appreciating the difference between a Class and an Object. Slow down!
 
 
subject: Have a class within a classs, how can I call setMethod from inner class from a separate class?
 
Similar Threads
Is there java class to parse xml nodes?
rich:tree working now... all the work has finally paid off - THANKS
Using SimpleXml, having issues with nested nesting collection within a collection
Trying to bind data to a TreeNode attribute using data read from a web service with simple xml
storing multiple attributes for one entity