• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

classes inside methods

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bah! Dastardly IDE's! :roll:
Thanks, Val.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Val you are right I have compiled it without problems
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic