• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access modifier question

 
Sumanth Shanbhag
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a directory called test and it has these two files in them.

1) A.java and
2) B.java

The code for A.java


The code for B.java


The A.Class file is in the directory test. When i try compiling B.java[while being in the directory test] it is not able to resolve A(compile time error). Could somebody tell me what is happening?
Where am i going wrong?
 
Shafian Kisna
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the instance, B is stored within the same package as A (Test). However since its in the same package, the error was called. From A.java, remove the package Test and run the program. It should not show any error.
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is looking for a directory test with a file A.java in it, but as you are already in the directory test, it cannot be found. I tried compiling it with:

javac -cp .. B.java

telling the compiler where to start looking for the directory test and everything went fine (after adding one semicolon and a curly brace to your code)
 
Sumanth Shanbhag
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shafian,

I am not too sure i got that. I am defining the package in A.java. Why would i have to remove that? You mean to say that A and B are not supposed to be in the same package(test)?
 
Shafian Kisna
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sumanth,

The reason why you do not need to specify the import is that, you are already placing B within the same package.
 
Sumanth Shanbhag
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sasha,

I was trying to figure out what "cp" meant in the compile statement?
Is it the CLASSPATH that you are mentioning? I tried using this to compile but it still gave that error.


>javac - classpath ## B.java

## = the pwd
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you have the package structure and directory structure wrong.

The directory structure must match the package structure. You have a class A in package test, which is in directory 'test'; your class B is not in package test, yet you have it in the same directory as class A.

You need to move B.java up one directory (it must not be in the 'test' directory, but one level up), or you must put class B in package test also (add a statement "package test;" to B.java and remove the "import test.A;").

The Java Tutorial - Packages
[ May 18, 2007: Message edited by: Jesper Young ]
 
Anita Singh
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You are telling that B.java is also in test package,but You are not using package statement there.

use package test;
in B.java also.
& remove import statement from there.

Then compile both the java file using below comand:-
javac -d . A.java
javac -d . B.java
it crete the folder test & put your class file there &
for running B.java write:-
java test.B

Anita Singh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic