Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing classes within packages.  RSS feed

 
JBaker
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Sorry to say I've got another newbie question.....I'm working through Ivor Hortons "Beginning Java 2 SDK 1.4 Edition". (Don't know if anyone has read this) I'm working through the exercises on "Defining Classes". Unfortunately, the exercises don't seem to come with any answers.
Basically, I've got a package called Geometry which contains two classes, Point and Line. A Line is made up from two Points. I'm creating a class called Rectangle, which is made up from two Points also (Top Left and Bottom Right). One of the exercises is to make a constructor method which creates a Rectangle from another Rectangle.
The problem is, I can't seem to access the Points public methods unless the class Rectangle is also part of the Geometry package by using: -
import Geometry.*;
I find this weird, because it obviously recognises the definition of the class Point for the Rectangle's private attributes.....I just can't access the methods (even though they are public).....the compiler complains about: -
(NetBeans error)
Cannot resolve symbol
symbol: method getX()
location: class Point
(JDK error)
Geometry/Rectangle.java [18:1] cannot resolve symbol
symbol : method getX ()
location: class Point
localPoint.setX(copyTopLeft.getX());
Now, this code works fine if the Rectangle class is also part of the Geometry package. Can someone tell me why this is the case and how I resolve it? Else using packages is going to be pretty useless if I cannot access the classes within the package properly from outside.
Thanks for any help.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds to me like you declared your getter/setters with default access and not public access. Methods with default access are only accessible from classes within the same package.
Given the Rectangle code:

If Point is defined as follows, you will get an error when compiling Rectangle:

But, if you change your method�s access attributes from default to public, all should work:

[ January 22, 2004: Message edited by: Mark Vender ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rage Matrix,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!