• Post Reply Bookmark Topic Watch Topic
  • New Topic

Innerclasses  RSS feed

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Error is giving but I want explaination...Why it cant call the method?
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!