aspose file tools*
The moose likes Beginning Java and the fly likes Protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected" Watch "Protected" New topic
Author

Protected

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479


I am trying to understand,how protected works in inner class of subclass in subclass-inside-the-same-package. I think,inside the same package,subclass,neighbor class /anyclass can access protected instances.

But here I am getting the error

Non-static variable this cannot referenced in the static context.

I couldn�t figure out how to correct this error. Please give me a helping hand.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Inner classes have an "enclosing instance." Each instance of Java_Study must be connected to an instance of the enclosing class Java_StudyNotes. In a non-static method of Java_StudyNotes, "this" would be used as that enclosing instance, but in main(), which is static, "this" is invalid. You can, instead, supply an instance using a special syntax. Since you've already created an instance in the variable "class2", you can use that one:

Java_Study javaStudy = class2.new Java_Study();

Now, let me give you a few pieces of advice. First, you're going to drive yourself crazy naming your classes and variables this way. A variable named "class2" should be used to hold an instance of java.lang.Class. If you called this instead javaStudyNotes, then it'd be much easier to understand what you're doing. It would also be consistent with the name "javaStudy". If you use the same convention to name all your variables, you'll be able to understand your own code better.

Second of all, get rid of the underscores in names. Common Java convention is never to use underscores in class names, and to use them in variable names only in CONSTANT_NAMES_LIKE_THIS .

And third, note that inner classes can access all of their enclosing class's members, even if they're private, and outer classes can access all of their inner class's members, even if they're private, and all classes in a package can access all the protected members of all other classes in that same package. So the experiments you're conducting are probably not going to teach you anything useful!


[Jess in Action][AskingGoodQuestions]
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
Hi Ernest,

Thank you very much for clarification and thanks again for correcting me to show me the right way to write coding conventions.I am working on it from now on.

Thanks
Shiva.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479

Inner classes have an "enclosing instance." Each instance of Java_Study must be connected to an instance of the enclosing class Java_StudyNotes. In a non-static method of Java_StudyNotes, "this" would be used as that enclosing instance


public class JavaStudyNotes extends Java{
void mohan(){
System.out.print(this.a);
this.testit();
}

I thought, this would be JavaStudyNotes.a and JavaStudyNotes.testit().Because,Subclass(JavaStudyNotes) inherit all the members from superclass.So subclass has it.

How could a non-static method of Java_StudyNotes, "this" would be used as that enclosing instance(superclass).Please clarify my doubt?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Go ahead and read this, this, and these study questions. There are some pictures and diagrams which I hope will explain things well enough.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
Thanks again, Ernest. I got that enclosing class is JavaStudyNotes(not the superclass ,in the above Example).I have one more doubt on this,Please forgive me for keep asking.

I am trying to understand ,what does a protected member look like to other classes trying to use the subclass-outside-the-package to get to the subclass inherited protected superclass member?

I wrote the coding



I got the cannot resolve symbol error.
Symbol:class Goo1
Location:class cert.JavaStudyNotes

Please help me to figure out.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Goo1 is an inner class of Goo, not of JavaStudyNotes; its enclosing instance must be of type Goo or a subclass of Goo.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
Thanks, I Got it.
 
wood burning stoves
 
subject: Protected