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
posted
0
Removed incorrect assertion
John.
David Sharpe
Ranch Hand
Joined: Jun 15, 2009
Posts: 32
posted
0
"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.
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
posted
0
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.
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 :)
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").
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: 2770
2
posted
0
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
posted
0
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!