aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQ+ Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQ+ Question" Watch "JQ+ Question" New topic
Author

JQ+ Question

padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
Question ID :954959230968
Which variables of the encapsulationg class, can an inner class access if the inner class is defined in an instance method of the encapsulating class?
options:select three correct answers:
// c refers to correct answers.
options;
a. All static variables ==c
b. All instance variables. -c
c. All final instance varibales. -c
d. All automatic varibales = wrong
e. All automatic final variables. == c
Well, I think the first one should be "All static instance variables" rather than just " All static variables" .
Anybody correct me if I am wrong. Also if there is a different opinion , please give an example too.
Thanks in advance
Padmini
Leena Diwan
Ranch Hand

Joined: Jun 18, 2001
Posts: 351
Hello Padmini,
if a class is defined inside a method, it can only access
the final variables.. not any others !
The answers you mentioned are probably for an member inner class.


[SCJP2, SCWCD1.3, SCBCD]
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Padmini, there is no such animal as a "static instance variable", by definition. The keyword "static" means "non-instance".
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Leena,
Just for the record:
An inner class defined inside a method can access all the variables of the enclosing class (static or non-static) and all final method variables. The only variables that it can't access are the non-final method variables.
Regards,
Manfred.
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
Originally posted by padmini Babu:
Question ID :954959230968
Which variables of the encapsulationg class, can an inner class access if the inner class is defined in an instance method of the encapsulating class?
options:select three correct answers:
// c refers to correct answers.
options;
a. All static variables ==c
b. All instance variables. -c
c. All final instance varibales. -c
d. All automatic varibales = wrong
e. All automatic final variables. == c

Padmini,
I'm a little bit confused about the answers you gave. what's the meaning of -c and == c?
I think the correct answers should be a,b,c,e. I mean 4, not 3.
What do you think?
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Hi everybody,
An inner class defined in the method can access all static and instance variables of its encapsulating class and all final variables of enclosing method.
so options a, b ,c and e should be right.
Please confirm if I am wrong.
Jyotsna
padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
Originally posted by Scott Appleton:
Padmini, there is no such animal as a "static instance variable", by definition. The keyword "static" means "non-instance".

Sorry Scott and others too.
Well, I think I used the wrong wordings for the problem.
One of the correct answers for this question is --- inner class can access all static variables. That 's the first answer ie, a
What I tried to do is use a static variable in a method and tried to access it from the inner class. It did not work. Hence, through a slip of wordings i used the word " static instance variable " .
what I actually wanted to ask is that a static variable in the enclosing class is only accessible and not a static variable from the enclosing method. Hence, the wordings of the answer should have been like - " All static variables of the enclosing class " instead of " All static variables " .
Anyway , the concept is clear. Thanks everybody.
Padmini

[This message has been edited by padmini Babu (edited June 21, 2001).]
Daniel Wu
Ranch Hand

Joined: Jun 13, 2001
Posts: 58
Originally posted by padmini Babu:

What I tried to do is use a static variable in a method and tried to access it from the inner class. It did not work. Hence, through a slip of wordings i used the word " static instance variable " .
what I actually wanted to ask is that a static variable in the enclosing class is only accessible and not a static variable from the enclosing method. Hence, the wordings of the answer should have been like - " All static variables of the enclosing class " instead of " All static variables " .
Anyway , the concept is clear. Thanks everybody.
Padmini
[This message has been edited by padmini Babu (edited June 21, 2001).]

Can a method declare a static variable? My answer is no. only class can declare static variables. Pls confirm me.
Daniel
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Daniel,
You're correct. A method cannot have a 'static' variable.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Rex Rock
Ranch Hand

Joined: Jun 25, 2001
Posts: 82
Hi,
I just thought I'll add some more points to the topic being discussed.
A class defined insided a method i.e a local class can be either static or non static.
If the method is static then the class defined insided the method also becomes static automatically.
If the method is non static then the class defined in such a method will the non static.
therefore we static local classes and non-static local classes.
A non-static local class can access all instance members and class members ( i.e static and non static members ) of the enclosing class and local final variables(and arguments passed to the method).
A static local class can access only static members of the enclosing class, it cannot access non-static memebers of the enclosing class, and ofcourse it can access local final vars and args.

Hope that helps,
Rex
Originally posted by padmini Babu:
Question ID :954959230968
Which variables of the encapsulationg class, can an inner class access if the inner class is defined in an instance method of the encapsulating class?
options:select three correct answers:
// c refers to correct answers.
options;
a. All static variables ==c
b. All instance variables. -c
c. All final instance varibales. -c
d. All automatic varibales = wrong
e. All automatic final variables. == c
Well, I think the first one should be "All static instance variables" rather than just " All static variables" .
Anybody correct me if I am wrong. Also if there is a different opinion , please give an example too.
Thanks in advance
Padmini

Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3313
    
    7
>If the method is static then the class defined insided the method also becomes static automatically.
This is not fully correct. An Anonymous class defined in a static method is still non-static.

------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Rex Rock
Ranch Hand

Joined: Jun 25, 2001
Posts: 82

Hi Paul,
Try this code to see what am saying
<pre>
class superclass {
public void method(){
//empty method;
}
}
public class sub extends superclass {
static int i = 3;
int j = 4;
static public superclass testanonymclassmethod() {
return new superclass() {
public void method(){
System.out.println(i);
System.out.println(j);

System.out.println("hello");
}
};
}
public static void main(String[] args){
new sub().testanonymclassmethod().method();
}
}
</pre>
In the testanonymclassmethod() we are creating a anonymous class. It will be a subclass of the class
called superclass.
We are overriding the method called 'method' ( sorry of the bad names here )
Since the method testanonymclassmethod is static the anonymous class defined will also be static.
As a proof of this..the compiler gives an error saying u are not allowed to access the variable j..
infact the error printed is this
" Can't make a static reference to nonstatic variable int j in void method().
System.out.println(j); "



However if you remove the word "static" from the method declaration you dont get the error and the program runs successfully.
Hope this helps
Rex
-----------------------------------------
If the method is static then the class defined insided the method also becomes static automatically.
This is not fully correct. An Anonymous class defined in a static method is still non-static.

Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3313
    
    7
Here, the anonymous class is created in a static reference and that's why it is not able to access instance member but that does not mean that the class itself is static.
A simple way to find out is:
After compiling your program, do to that directory from dos prompt/shell and see the name of the anonymous class. It would be (most probab) Sub$1.class
Now do: javap Sub$1
This will show you whether this class is static or not.
For your conv. I'm putting a small code that illustrates the difference:

Following is the javap output:
=============================
C:\TEMP\java>javap TestClass$1
Compiled from TestClass.java
class TestClass$1 extends java.lang.Object implements java.awt.event.ActionListner {
TestClass$1();
public void actionPerformed(java.awt.event.ActionEvent);
}
C:\TEMP\java>javap TestClass$StaticInner
Compiled from TestClass.java
public static class TestClass. StaticInner extends java.lang.Object
/* ACC_SUPER bit NOT set */
{
public TestClass.StaticInner();
}
C:\TEMP\java>
=============================
Do you see the diff.?
HTH,
Paul.

------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
Paul does this mean that anonymous inner class would never be static? I was reading another post and then I remembered this post of yours. I tried creating an anonymous class within a static block and without a static block and in both cases javap told me that class was final. Infact it gave me identical output in both cases.
Please take a look at this post and comment. http://www.javaranch.com/ubb/Forum24/HTML/010760.html
regards,


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Khalid said that an anonymous class created in a static method is indeed static, not only because it is unable to access the enclosing class's instance variables, but also that the outer class was not necessarily instantiated to create such an anonymous class.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3313
    
    7
>Khalid said that an anonymous class created in a static method is indeed static,
1. Mr. Khalid did not say that at all. http://www.javaranch.com/ubb/Forum24/HTML/010760.html
>not only because it is unable to access the enclosing class's instance variables,
2. It is not able to acces enclosing class's instance variables not because it is static but because those variables do not exist it's context!
>but also that the outer class was not necessarily instantiated to create such an anonymous class.
3. Creating anonymous class in a static method is just another language feature. In this case, existance of outer class is not needed... by language design/rule/Gosling et all's wish!!!
Anyway, people are just trying to make a big fuss about it. JLS clearly specifies that Anonymous class can never be static. And they mean it that's why it does not say that Anonymous class can never be declared static.
What more proof do you need? Compile a few lines of code see it for yourself!
-Paul.

------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
wood burning stoves
 
subject: JQ+ Question