• 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

Innerclasses

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why we cant call a method in innerclass of outerclass?
e.g:
class A{
static class Inner{
void meth(){
System.out.println("method called");
}
}
class Main{
static class Inner{
void meth(){
System.out.println("method called");
}
}
public static void main(String args[]){
Main m=new Main();
Inner in=new Inner();
in.meth();
// UP to this it executes... i.e it calls Main inner class meth().
//but failed follwoing.
A a=new A();
a.in.meth();
}
}
And also
Why we cant call method from innerclass....?

e.g
class A{
static class Inner{
void meth(){
S.o.p(" Inner method of A called");
}
static class InInner{
Main m=new Main();
m.meth1();
}
}
}
class Main extends A{
void meth1(){
S.o.p("meth1 called " );
}

public static void main(String args[]){
Main m=new Main();
}
}

plz check the paranthesis.....
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line

a.in.meth();

does not work because there is no member variable called "in" in class A (the compiler error message tells you that when you try to compile it).

The method meth() in the inner class in class A is not static. You could call it by creating a new instance of the inner class and calling the method on it:

A.Inner ai = new A.Inner();
ai.meth();

Look at the compiler error messages, they tell you what's going on.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, A STATIC class is actually NOT an inner class as it doesn't inherently acquire access to all the members of the class its enclosed in..also, check out the access issues from a static context.
 
Ram Hebbale Hiriyanna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BigOuter.Nested n = new BigOuter.Nested(); this is how you instantiate a static enclosed class.......where the Nested class is the static top level nested class. try incorporating this in your code.....
 
amit bhadre
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Error is giving but I want explaination...Why it cant call the method?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want an explanation you need to ask one question at a time, and ask it clearly enough so that we know what we're discussing. Your example uses a member variable "in" which doesn't exist anywhere, in any class; as a result, we don't know what you're trying to ask! Also using "pseudocode" like "S.o.p" makes things needlessly hard to follow; see here.

JavaRanch also offers [ CODE ] [/ CODE ] tags, which you can surround your code with to preserve the formatting. That would also help us understand what you're trying to say by making your code readable.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic