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

non serializable session websocket java ee
Embed the existing code of a method in a try-finally block (2)
Trouble with repainting the screen, but not clearing previous shapes
Who does the arithmetic and logic operations in the Java or C++ source code? [closed]
IllegalArgumentException : Comparison method violates it's general contract
javax.net.ssl.SSLHandshakeException: Received fatal alert: unknown_ca
DELETE Function in UcanAccess throws strange exception
Need to execute a piece of code repeatedly for fixed duration in java
I do not understand why when I run this code it never goes into the onScannedRobot(ScannedRobot e) method?
If there is no wljarbuilder.jar file on a weblogic installation, how else can you generate a wlfullclient.jar file?
Upload functionality using Robot class in selenium Web Driver
Use of Interfaces on a service layer
Class can`t declare type
How to read content of file stored on server using java? [duplicate]
Reading properties file from server location
Eclipse - Program Breaks when Exported

Categories

HOME
compiler-construction
pandas
bluetooth
clips
c#-4.0
view
vmware
jira
razor
v8
callback
mvvmcross
facebook-messenger-bot
elasticsearch-hadoop
dax
timeout
uitypeeditor
highlight.js
finite-automata
invantive-sql
swingx
java-7
django-storage
wpfdatagrid
internet-explorer-9
entitlements
windows-server-2000
nameservers
binary-data
key-value-observing
xquery-3.0
pdb
code-contracts
caret
android-nestedscrollview
bower-install
serve
espeak
serverside-rendering
occlusion
code-search-engine
tasker
ncalc
pdf-reactor
firebase-admin
outlook-api
reactive-cocoa-5
slick-3.0
lowpass-filter
zendesk-app
imanage
static-ip-address
rotativa
trash
merge-conflict-resolution
superstack
itextpdf
thredds
spidermonkey
libressl
ibaction
responsive-images
iiviewdeckcontroller
clipperlib
apache-commons-net
csquery
xc16
qpainter
gadt
coldbox
heisenbug
aapt
navigationservice
system.net.webexception
rtmfp
jboss-weld
concurrent-collections
fmod
runtime.exec
yui-compressor
http-unit
word-processor
newtonscript
osql
hgsubversion
transactionscope
mercurial-server
gcj
horizontal-accordion
celltable
sortable-tables
ext3
meego
exchange-server-2003
eqatec
sustainable-pace
zend-decorators
port-number
wsdl.exe
3gp

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