• 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

newbie question

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi , i am a newbie...i have some simple question


question... i am not comfortable with the syntax
outer.this.setValue(newOther);...is this legal? does it have any diffrence with outer.setValue(newOther); ?
another question..
class C {
class D {
}
}
C c = new C() //ok
D d = c.new D()---->how this works...this syntax is not understandable.

an object c is dotting(.)with other object new D() !! its surprising to me bcoz object creation rule is
classname objectname = new classname() right?
but that is not obeying this rule!
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The questions u faced are really very common for programmers. There may be many other ways to solve your problems. But, I have some solutions.
Sol 1.
The syntax you gave is illegal. And I suggest to use 'super' keyword by extending your inner class to outer and then access 'super.setValue(Object obj).
Sol 2.
Since the class D is inner to C, D is local only to C. So, u can't create object in the main method directly. You follow this.
1. Create Object for class C. // C c=new C()
2. Get the object of the inner class. //c.new D()
3. If you want to assign the above to a particular object, then define like this(Since Object is the super class of all classes). // Object ob=c.new D()
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The questions u faced are really very common for programmers. There may be many other ways to solve your problems. But, I have some solutions.
Sol 1.
The syntax you gave is illegal. And I suggest to use 'super' keyword by extending your inner class to outer and then access 'super.setValue(Object obj).
Sol 2.
Since the class D is inner to C, D is local only to C. So, u can't create object in the main method directly. You follow this.
1. Create Object for class C. // C c=new C()
2. Get the object of the inner class. //c.new D()
3. If you want to assign the above to a particular object, then define like this(Since Object is the super class of all classes). // Object ob=c.new D()
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution I gave for 2nd question may confuse you. You can get access to the member in inner class by writing 'c.new D().member'.
If you want to assign the above it to a particular object, u declare like this.
C.D obj=c.new D(); // outerCLASS.innerCLASS innerOBJECT= outerOBJECT.new innerCLASS()
And you can access the inner member by obj.member
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ssrc,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to take a look at the "Getting in Touch with your Inner Class" article of the JavaRanch Campfire Stories.
 
A feeble attempt to tell you about our stuff that makes us money
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic