• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newbie question

 
salvador rcn
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!
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • 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
Java Notepad Tomcat Server
  • 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
Java Notepad Tomcat Server
  • 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
 
Dirk Schreckmann
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic