File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes classes inside methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "classes inside methods" Watch "classes inside methods" New topic
Author

classes inside methods

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello Guys
Here is a question

The BaseClass and ChildOfInnerBase are in different package, the class inside the method is able to access to hello a private instance variable of ChildInnerBase, but is not able to access x a protected instance variable of BaseClass. why?
Amish :roll:
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Amish,
I tried your code and it does not produce any compiler error. I've added a main method to ChildOfInnerBase so that I can run the program and everything is fine.
The output is:
x is 100
hello is 0
x is 100
Here is the modified code:

Are you sure you have your classpath correct?
[ February 13, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Valentin Crettaz:
Amish,
I tried your code and it does not produce any compiler error. I've added a main method to ChildOfInnerBase so that I can run the program and everything is fine.
The output is:
x is 100
hello is 0
x is 100
Are you sure you have your classpath correct?

Valentin,
I tried something very similar and I got the same error Amish is seeing, but I can't explain why. As far as I can tell, this should compile and run. This is the code I used:

When I try to compile this, I get the following error message at line "1": "The field named parentMember for type named bottom.Child is not visible."
I'm pretty sure this isn't a classpath problem, however, I don't know why this causes a compiler error.
If you notice, I can access Parent.parentMember outside of the local class, just not inside it.
Thanks,
Corey
[ February 13, 2002: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
One other thing I've noticed is that, if the parent class is in the same package as the child class, no compiler error is produced. If you move the parent to a different package, however, the error appears. Also, if you make the parent member that you're trying to access public, rather than protected, the compiler error goes away.
Can anyone explain why this is? Since the member is protected, it should be considered "part of" the child and, therefore, the local class should have access to it, right?
Thanks,
Corey
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Val,
I was trying to compile this code in Visual Age for Java v 3.5 and it was not compiling so I made this post, then I realized that I should compile it with Sun's JDK and VAJ uses IBM's JDK and still I am getting the following error..
inheritance/ChildOfInnerBase.java:9: cannot resolve symbol
symbol : class BaseClass
location: package InnerClasses
public class ChildOfInnerBase extends InnerClasses.BaseClass {
^
inheritance/ChildOfInnerBase.java:14: cannot resolve symbol
symbol : variable x
location: class Inheritance.ChildOfInnerBase
System.out.println("x is " + x);
^
inheritance/ChildOfInnerBase.java:19: cannot resolve symbol
symbol : variable x
location: class aMethodClass
System.out.println("x is " + x);
^
3 errors
Please forgive me if I am not allowed to post errors in this forums.
Amish
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Corey McGlone:
One other thing I've noticed is that, if the parent class is in the same package as the child class, no compiler error is produced. If you move the parent to a different package, however, the error appears. Also, if you make the parent member that you're trying to access public, rather than protected, the compiler error goes away.
Can anyone explain why this is? Since the member is protected, it should be considered "part of" the child and, therefore, the local class should have access to it, right?
Thanks,
Corey


Corey
U r right on this since since it is an instance inherited variable the local class should have access to it, I also do not why it is not compiling. Not in VAJ 3.5 or Sun JDK 1.3.1.

:roll: Amish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Corey,
I have no difficulties compiling your code either. There is something weird...
I've tried both examples (Amish's and yours) with both JDK 1.3.1 and JDK 1.4 b2...
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Valentin Crettaz:
Corey,
I have no difficulties compiling your code either. There is something weird...
I've tried both examples (Amish's and yours) with both JDK 1.3.1 and JDK 1.4 b2...

Hmmm... I'm also using VAJ 3.5, so it might be a problem with that, but Amish said that he was unable to compile it using the Sun JDK, as well.
Just out of curiosity, Amish, what version of the Sun JDK did you try to compile it with?
Corey
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Amish that's definitely a classpath problem. When you compile you should specify the "-classpath" switch correctly to avoid such weird things...
my hierarchy is
test
test/Inheritance/ChildOfInnerBase.java
test/InnerClasses/BaseClass.java
What I did:
1. go to InnerClasses directory
2. javac -classpath .. *.java
3. go to Inheritance directory
4. javac -classpath .. *.java
5. java -classpath .. Inheritance.ChildOfInnerBase
and everything is fine
The same for you Corey...
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Bah! Dastardly IDE's! :roll:
Thanks, Val.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Ok here is my synthesis. I don't pretend that it is right, but it is how I understand the problem.
I'll take Corey's example:
We have two packages top and bottom.
The former contains the class Parent and the latter the class Child.
Parent defines a protected member (parentMember) which can be inherited by subclasses (or classes in the same package). Child subclasses Parent and inherits parentMember which is available in the whole body of the declaration of the class Child, that is, at the same level than childMember. So since the local class instanceMethodClass can access childMember it can also access parentMember.
Am I nuts or what?
How is it going for you guys now? Does it compile?
[ February 13, 2002: Message edited by: Valentin Crettaz ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Valentin Crettaz:
Amish that's definitely a classpath problem. When you compile you should specify the "-classpath" switch correctly to avoid such weird things...
my hierarchy is
test
test/Inheritance/ChildOfInnerBase.java
test/InnerClasses/BaseClass.java
What I did:
1. go to InnerClasses directory
2. javac -classpath .. *.java
3. go to Inheritance directory
4. javac -classpath .. *.java
5. java -classpath .. Inheritance.ChildOfInnerBase
and everything is fine
The same for you Corey...

Val
I am able to perform your steps 1, 2, 3, but step 4 I still get the error I reported to u earlier. I did a set CLASSPATH and just for the heck of it, I reconfigured my classpath to
set CLASSPATH=.;
and tried to do the same thing and it still gives me error.
Correct me if I am wrong, from jdk1.3 onwards we do not have to set the classpath to point to rt.jar? Am I right?
Still I do not understand?
Amish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Amish,
when you are in any of your package the classpath should be set to the parent directory, hence the .. (note that there are 2 dots).
In the classpath you set, I can only see 1 dot.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Corey McGlone:
Bah! Dastardly IDE's! :roll:
Thanks, Val.

Well also why is it in VAJ 3.5 we are able to access x outside of the local class and not inside the local classes method??? I really do not think this is a class path problem. Please verify this Corey???
Amish
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Valentin Crettaz:
Amish,
when you are in any of your package the classpath should be set to the parent directory, hence the .. (note that there are 2 dots).
In the classpath you set, I can only see 1 dot.


Val
What I mean in that quote is first I reset the CLASSPATH so that it is .; and then I did your steps 1, 2, 3 and at 4 I got the same error.
I hope this clarifies it.
Amish
Now this forum says that the flood control has been activated and I have to wait for 30 sec. Hence I am waiting. ---- Sorry -----
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Well, like I said it's weird...
The "cannot resolve symbol" thing is often a classpath-related issue...
I don't know what I could add...
(Flood control what ???)
[ February 13, 2002: Message edited by: Valentin Crettaz ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Valentin Crettaz:
Well, like I said it's weird...
The "cannot resolve symbol" thing is often a classpath-related issue...
I don't know what I could add...
(Flood control what ???)
[ February 13, 2002: Message edited by: Valentin Crettaz ]

Val,
I am so sorry, I just figured out what the problem is, I had named my directories
c:\myFiles\innerclasses
c:\myFiles\inheritance
and in my package declaration I had
Innerclasses and Inheritance.
I am soo sorry, this is my silly mistake. I sincerely apologize for this.
Although I did found out that in VAJ 3.5 it gives an error if I put the
System.out.println("x is " + x );
inside the local class's method. If I put the above statement in a local method it works fine. Just one of the many differences in IBM's jdk and Sun's JDK.
Also whenever you try to post something, you have to wait for 30 secs between two consecutive post or else javaranch will tell flow control error and u have to wait for 30 secs.
Once again Sorry!!!
Amish
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi all
I haven't got a compiler at hand now , but this could help.
Access to protected members outside the package is allowed based on the type of the reference used to acces them. Inside the inner class the compile type of "this" is the inner class itself. But inner class is not a subclass of the class that defined the protected member. Thus access should no be allowed.


SCJP2. Please Indent your code using UBB Code
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Jose,
I agree with you but the thing is that the enclosing class inherits the member, and local classes can usually access the enclosing class members... So?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Amish,
glad you solved the problem...
(I never get that "flood" thing... huh, weird )
[ February 13, 2002: Message edited by: Valentin Crettaz ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Val you are right I have compiled it without problems
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classes inside methods
 
Similar Threads
Polymorphic construction
private methods
Inner classes
Private variables in superclass accessed by subclass
importing packages in a class