Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to access the method in such a inner class?

Reply
Thread Tools

How to access the method in such a inner class?

 
 
Bruce Sam
Guest
Posts: n/a
 
      01-12-2005
/* ************************* */
public class A {
public void g() {
class B {
public void f() {
System.out.println("f()");
}
}
}
public static void main(String[] args) {
}
}
/* ************************** */
Above program is an incomplete code.The code I want to add is that
access the meothod of f();But I don't know how to access the method f()
in such a inner class?

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      01-12-2005
On 12 Jan 2005 00:35:23 -0800, Bruce Sam wrote:

....
> public class A {
> public void g() {


Please indent any code posted.

....
> Above program is an incomplete code.The code ...


Putting ' ' after a full stop (.) would help as well. Otherwise
sentences run together. It is harder to read them.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
 
 
 
John English
Guest
Posts: n/a
 
      01-12-2005
Bruce Sam wrote:

> /* ************************* */
> public class A {
> public void g() {
> class B {
> public void f() {
> System.out.println("f()");
> }
> }
> }
> public static void main(String[] args) {
> }
> }
> /* ************************** */
> Above program is an incomplete code.The code I want to add is that
> access the meothod of f();But I don't know how to access the method f()
> in such a inner class?


In g():
B b = new B();
b.f();

-----------------------------------------------------------------
John English | (E-Mail Removed)
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
 
Reply With Quote
 
Bruce Sam
Guest
Posts: n/a
 
      01-12-2005
Hello John,It's not clear that what I said before,I want to access the
method f( ) in the main( ).

 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      01-12-2005
Bruce Sam wrote:

> Hello John,It's not clear that what I said before,I want to access the
> method f( ) in the main( ).


Put this code, based on what John English gave you, into main():

B b = (new A()).new B();
b.f();

Or do exactly as John suggested and also put this in main():

A a = new A();
a.g();

The main method is static; you cannot invoke instance methods without an
instance to invoke it on. Furthermore, B is an inner class of A; you
cannot have an instance of B without a containing instance of A. An
alternative, then, would be to make B a static nested class and make f()
a static method of B, in which case you could simply do this from main():

B.f();

Unless this is purely hypothetical, program design considerations will
govern which approach is most appropriate, and indeed, whether invoking
B.f from A.main() is appropriate at all.


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Inner classes and method signatures Bjorn-Ove.Heimsund@uib.no Java 3 02-19-2004 02:05 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM
Static inner class not seeing super class method kishan bisht Java 2 07-02-2003 02:15 PM



Advertisments