Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Removed incorrect assertion

John.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :)
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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").
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3626
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic