English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

No! A method has access only to class members not to other method's components even if they are created in the same class.
The reason is simple.. The variables you're using for example in method1 are instantiated only when you use method1 and after finishing with it they will be deleted... So there's no way you can access them from other method.. They only exist in the method where they were declared!

If you meant accessing other methods and not the code inside them, then it can be done using this operator :). This is how I understood it first!

2007-03-06 09:45:47 · answer #1 · answered by agent-X 6 · 0 0

Yes. Because you are using the method in the method which causes you to access classes that you have already defined!

2007-03-06 17:45:58 · answer #2 · answered by Computer Genius 1 · 0 0

Agent-X’s answer confuses me. He seems to be answering “can code from one method directly access code in another method”. Is that correct agent-X? But, that isn’t how I read this question. I read can one method call another.


In short, yes:

this.method();

You can access any method or field in the class from within the class or super class as long as they are in the same scope, class or not class. They are class members.

Not all combinations of instance and class variables and methods are allowed:
•Instance methods can access instance variables and instance methods directly.
•Instance methods can access class variables and class methods directly.
•Class methods can access class variables and class methods directly.
•Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.

2.8.4 The Class Members
The members of a class type include all of the following:

* Members inherited from its direct superclass (§2.8.3), except in class Object, which has no direct superclass.

* Members inherited from any direct superinterfaces (§2.13.2).

* Members declared in the body of the class.

Am I missing something?


Read the JVM spec, it is awesome.

2007-03-06 18:14:05 · answer #3 · answered by saxondog 3 · 0 0

fedest.com, questions and answers