• 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

JQ+ Question

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Padmini, there is no such animal as a "static instance variable", by definition. The keyword "static" means "non-instance".
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daniel,
You're correct. A method cannot have a 'static' variable.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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
 
Rex Rock
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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
reply
    Bookmark Topic Watch Topic
  • New Topic