• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

visibility

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a folder called "kathy" on my "desktop", I creates a file called "chapter1exercise1" I am using Textpad and compile the code using the Textpad > Tools




A sub-folder is created inside "kathy" folder called "new1". The following file "another" is created inside this folder "new1"



I am messing up with package declaration here but cannot figure out how. Any clues ?

Compile error

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chapter1exercise1 is a public class.. why is it not visible ?
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
What might be the problem is that the kathy package is not set into the classpath variable. Just try using the DOS Window and while compiling set the classpath to the kathy package.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
OK, Arnb.. Here's what I did. I made a few changes here and there to get the following results:


In the above code I changed the scope of the test() method from the default scope("friendly") to public. This was done so that the abc() method of another class had the visibility to see this method.




In the above code, I made the class public so that it could be accessible to another class "test". This was done only for testing purposes.
I added a constructor so that the abc() method could be called for testing purposes.



The above class was made for testing purposes only.

Now the structure of file directory is like this:
kathy>>new1>>another.java
kathy>>chapter1exercise1.java
kathy>>test.java

Now in the command line of DOS Window I first navigated inside the kathy folder.
G:\kathy>javac -cp .;.. test.java

G:\kathy>java -cp .;.. test

And the Output was as expected:
in test

NOTE: in "-cp" --- "." refers to current directory
and ".." refers to parent directory
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm.. so it was more of class path issue ??

But even without changing the access modifiers, will it work when compiled from DOS prompt ? I will try it out!
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Arnb,
Like I told you before you will have to change the access modifier of test() method to public because the default modifier does NOT allow access the package. The other changes are not necessary.
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shyam,

That is correct.

I compiled it from dos prompt with the following command when I was in the folder "new1"



and that clears my doubt !
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that you're getting the hang of things, see if THIS helps.
 
zhu pan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
--------------------------------------------------------------------------------

package kathy;public class chapter1exercise1 { public static void main(String args[]) { System.out.println("in main"); } public void test() { System.out.println("in test"); }}

--------------------------------------------------------------------------------




A sub-folder is created inside "kathy" folder called "new1". The following file "another" is created inside this folder "new1"


code:
--------------------------------------------------------------------------------

package kathy.new1; import kathy.*; class another extends chapter1exercise1 { void abc() { test(); }}

--------------------------------------------------------------------------------
[ December 25, 2005: Message edited by: Marilyn de Queiroz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic