java


why one class of same package can't access another?


package MyPack;
class Balance
{
String name;
protected double bal;
Balance(String n, double b)
{
name=n;
bal=b;
}
void show()
{
if(bal<0)
System.out.print("--> ");
System.out.println(name+": $" +bal);
}
}
class : AccountBalance
package MyPack;
class AccountBalance
{
public static void main(String[] args)
{
Balance current[]=new Balance[3];
current[0]=new Balance("K. J. Fielding", 123.23);
current[1]=new Balance("will Tell", 157.02);
current[2]=new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++) current[i].show();
}
}
I have put both these classes in Balance.java and AccountBalance.java . both files are in E:/programs/MyPack . Balance.java compiles without error But when I compile AccountBalance.java it gives error : cannot find symbol "Balance".
I'm unable to figure out why when both classes are declared in same package?
I'm compiling from MyPack using
javac Balance.java
javac AccountBalance.java
Assuming you're issuing your javac command from some folder other than E:/programs, you'll need to specify a -cp option including the location that includes your Balance class.
This is because javac uses the current directory if the option isn't specified
If neither CLASSPATH, -cp nor -classpath is specified, the user class
path consists of the current directory.
So if you did, for example,
E:/> javac programs/MyPack/AccountBalance.java
then the Balance class will not be in the classpath and the compiler will give you the error you see.
In that case, you'll need to specify an explicit location for your classpath. For example
E:/> javac -cp programs programs/MyPack/AccountBalance.java
Since Balance is in package MyPack which is at the root of /E/programs, the compiler finds it and can use it.
Use an IDE.
Assuming you use javac the reason is that you compile them one by one (first Balance then AccountBalance) and you are not in the parent folder of MyPack. If you are really doing so, then please use the -cp option of javac to point where is the already compiled Balance.class. For example:
..\so\src\MyPack>javac Balance.java
..\so\src\MyPack>javac -cp ../. AccountBalance.java
Alternatively compule them both, e.g:
..\so\src\MyPack>javac *.java

Related Links

Butterknife #genarate not working{Error:(23, 6) error: cannot find symbol class Bind}
Java: Constraint programming library suggestion?
isSelected() Checkbox error
Use of str.length()-1 in case of swapping a letter of a word with each other [closed]
what will cause socket write throws connection timeout exception
Apache POI get line breaks from XWPFRun
Android Display 2 fragments inside another one
Dynamic addition of column in TableView and editing cells using javafx
Java Reflection : invoking inherited methods from child class
If Else Condition not Working in Increase and Decrease Value of qty
For each save (update) hibernate envers creating revisions either changed in table or not
Starting Tomcat with CMD
org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
Assert 2 Lists<String> are equal ignoring order
NoSuchMethodException SoapUI Java
Why can this object access methods of it's parent class?

Categories

HOME
pandas
zeromq
image-processing
minimum-spanning-tree
mediawiki
fluentd
pycharm
hashmap
iot
relayjs
relay
bpmn
rdf
ravendb
session-variables
tomcat6
export-to-csv
php-7.1
spring-tool-suite
usergrid
serverless-framework
seaborn
pythonanywhere
code-review
firefox-webextensions
jtextfield
ehcache
web-sql
chromium-embedded
language-agnostic
trading
user-interaction
strncpy
ping
buck
msys2
procdump
skia
frame
nameservers
iframe-resizer
accelerate-framework
leading-zero
opshub
squib
hp-ux
ruby-on-rails-3.1
node-gyp
eclipse-gef
wptoolkit
auto-update
hls.js
statsd
mplayer
magma
topbeat
color-picker
nxt
grails-tomcat-plugin
slicknav
dstu2-fhir
design-by-contract
photobucket
rtbkit
ios8-today-widget
spim
execute
metaclass
jmeter-maven-plugin
responsive-images
ideamart
nsight
iiviewdeckcontroller
pretty-print
proj4js
nsmutabledictionary
braille
openlaszlo
ccss
tidy
flash-builder4.5
jplaton
cos
mylyn
lcs
trusted
google-email-migration
ticoredatasync
windows-phone-7.1.1
gnu-prolog
xtype
appender
radcombobox
suppress
forums
dbal
database-management
compiler-specific
anti-piracy
pascal-fc
scripting-languages
ugc
rtml
misv

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App