• 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

How to use innerclass?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that if I declare innerclass I must use the form
" outerclass.innerclass a = new outerclass().new innerclass() "
but why program underbelow complie and run without wrong???
class Q40 {
static int x=9;
public static void main(String[] args) {
Q40 test = new Q40();
test.methodA();
}

void methodA() {
Process p = new Process();
p.processMethod();
System.out.println("Value of x = " + x);
}

class Process {
void processMethod() {
System.out.println(-0.0 == +0.0);
System.out.println("Value of x = " + x);
}
}
}
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jinfeng,
Good Question!I executed this program and tried javap from command prompt.
Here's the result.
G:\gudiya\Trials>javap Q40
Compiled from Q40.java
class Q40 extends java.lang.Object
/* ACC_SUPER bit set */
{
static int x;
static static {};
Q40();
public static void main(java.lang.String[]);
void methodA();
Compiled from Q40.java
class Q40.Process extends java.lang.Object {
Q40.Process(Q40); //*****************You see the inner class is indeed accessed with an instance of the outer class only
void processMethod();
}
}
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jinfeng yin
What u are saying is right but only when the access is done from another class - Priya has given reply to why it works in yr code - so I have modified yr code slightly to clear the issue. hope it answers yr questn
class Q40 {
public static void main(String[] args) {
Q41 test = new Q41();
Q41.Proc QP = new Q41().new Proc();
test.methodA();
QP.procMethod();
}
}
class Q41
{static int x=9;
void methodA() {
System.out.println("Value of x = " + x);
}
class Proc {
void procMethod() {
System.out.println(-0.0 == +0.0);
System.out.println("Value of x = " + x);
}
}
};
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the funda is from a non-static method in a class (instance method), you can directly instantiate a non-static inner member class (similar to access to an instance variable).
But from static methods, if you want to instantiate it, you have to use the instance of the outer class.
Hope this is the other version of what priya and shailendra interpreted it!.
reply
    Bookmark Topic Watch Topic
  • New Topic