This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Quick Question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True or False?
An instance of a non static inner class always has an associated instance of the enclosing class.
I think it's false because a non static inner class may be a inner class defined in a method. A inner class defined in a method doesn't have an associated instance of the enclosing class, correct?
Ada
 
Ada Wang
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone tell true or false?
Ada
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Complie and run the following code...
//===================================================
public class MethodInnerClassTest
{
String str = "Outer Class";
public void someMethod()
{
class Inner
{
public void im1()
{
System.out.println(" Accessing outer's fields "+MethodInnerClassTest.this.str);
}
}
Inner in = new Inner();
in.im1();
}
public static void main(String[] args)
{
MethodInnerClassTest mt = new MethodInnerClassTest();
mt.someMethod();
}
}
//===================================================
This defines and instantiates an inner class in a method and then tries to access our class's field.
This shows that even if the inner class is inside a method, it has a reference to it's outer class instance.

------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!
 
Ada Wang
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. To access the inner class of a method, we must create an instance of the enclosing class, from which we may access the method and the inner class of it.
Thanks Paul.
Ada
 
Ada Wang
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
I modified your code. This time we don't have to create any instances of the enclosing class.
===========================================================
public class MethodInnerClassTest{

static String str = "Outer Class";

public static void someMethod(){
class Inner{
public void im1(){
System.out.println(" Accessing outer's fields " + MethodInnerClassTest.str);
}
}
Inner in = new Inner();
in.im1();
}
public static void main(String[] args){
//MethodInnerClassTest mt = new MethodInnerClassTest();
//mt.someMethod();
someMethod();
}
}
==========================================================
From above, the answer should be false.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When u changed u made the method to be static. So it can be called directly without an instance of the outer object. In fact this can be done directly by declaring the inner class itself static. So the answer is basically true not false.
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!