Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help on accessing the method

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can anyone tell how to access the getPoint() method.
Please..
I think we can acess like this :
Line l = new Line();
l.getPoint();

Source is my own
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following should do the trick:

The thing to know is that you need an instance of the outer class before you can create an instance of the inner class. (That's what the odd-looking "line.new Point()" syntax is all about.)
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we are accessing the inner class outside outer class.
Correct me if iam wrong..
 
Keith Nagle
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right.
Accessing the inner class outside of the enclosing class.
 
Saurabh V Vakil
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dinesh Tahiliani:
I think we are accessing the inner class outside outer class.
Correct me if iam wrong..


you aren't accessing inner class(Point) outside outer class(Line) as main method is a part of the Line class.
you need an instance of class Point to access the getPoint() method, so l.getPoint() won't do since l is not an instance of Point. you can try

new Line().new Point().getPoint()

or

Line l=new Line();
Line.Point p=l.new Point();
p.getPoint();
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you call method of inner class within enclosing class then

Line line = new Line();
Point p = line.new Point();
p.getPoint();

If you call method of inner class outside to enclosing class then

Line line = new Line();
Line.Point p = line.new Point();
p.getPoint();

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic